From cs at cskk.id.au Mon Apr 1 18:45:21 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 2 Apr 2019 09:45:21 +1100 Subject: [Tutor] Python Django Query In-Reply-To: References: Message-ID: <20190401224521.GA71863@cskk.homeip.net> On 29Mar2019 21:18, nitin chandra wrote: >Hi Everyone, >I need some guidance / corrections to my code. >I hosted a demo site on pythonanywhere.com and to test the initial >pages I wrote some test code. [...] Hi Nitin, I looks like nobody has replied; this probably means that nobody has the required specialty expertise to help you. (I don't.) >Good news, put in 10 hrs, tried so many suggestion given on the net, >yet non worked. So finally mailing the list. Might I suggest that you ask on the django-users mailing list, here: https://www.djangoproject.com/community/ You want the "Using Django" list there. There's a guideline to posting here: https://code.djangoproject.com/wiki/UsingTheMailingList but your post looks fairly good on the face of it. Hoping this helps, Cameron Simpson > >Objective : display main page with multiple links to other pages. Use >jQuery plugins, click the link on main page and open other pages. > >The following path and pages are posted below. > >Thanks > >Nitin Chandra > >------------------------------------------------------------ >ERROR >When I click on 'Table' link on 'home' page, the following error is displayed > >http://selftech.pythonanywhere.com/table.html >Page not found (404) > >Request Method:GET >Request URL:http://selftech.pythonanywhere.com/table.html > >Using the URLconf defined in eduinfra.urls, Django tried these URL >patterns, in this order: > >admin/ >[name='home.html'] > >The current path, table.html, didn't match any of these. > >You're seeing this error because you have DEBUG = True in your Django >settings file. Change that to False, and Django will display a >standard 404 page. > >+++++++++++++++++++++++++++++++++++ > >/home/selfTech/edu/tech/ > > __init__.py > admin.py > apps.py > models.py > tests.py > urls.py > views.py > >/home/selfTech/edu/tech/templates/ > > base.html > home.html > table.html >******************************************* >our 'base.html' code below (test code, gets rendered correctly) > > > ************************* >home.html > >{% extends 'base.html' %} > >{% block content%} >Hello TEAM :) >{% endblock %} >******************************* >table.html (gives ERROR 404) > > > > > > > >
Name
> >********************************************* >/home/selfTech/edu/tech/ > >urls.py > >from django.urls import path >from . import views > >urlpatterns = [ > path('', views.home, name='home'), > path('table/', views.table, name='table') ## various combo did not work > # path (' ', views.table, name='table') ## nope > # path (' ', views.table, name='table.html') # nope >] > >*************************/////////////////////// >views.py > >from django.shortcuts import render >from django.http import HttpResponse ># Create your views here. > >def home (request): /// correctly renders > return render (request, "home.html",{}) > >def table (request): //// gives error > return HttpResponse ('

This is a test Page

') > # Tried the below code, nada > # return render (request, "table.html",{}) > >*************************\\\\\\\\\\\\\\\\\\\\\\\\\ >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor From nitinchandra1 at gmail.com Tue Apr 2 07:50:06 2019 From: nitinchandra1 at gmail.com (nitin chandra) Date: Tue, 2 Apr 2019 17:20:06 +0530 Subject: [Tutor] Python Django Query In-Reply-To: <20190401224521.GA71863@cskk.homeip.net> References: <20190401224521.GA71863@cskk.homeip.net> Message-ID: Thanks Cameron, Just got lucky yesterday and found a resource at my end to help me out. Thank you On Tue, 2 Apr 2019 at 04:31, Cameron Simpson wrote: > > On 29Mar2019 21:18, nitin chandra wrote: > >Hi Everyone, > >I need some guidance / corrections to my code. > >I hosted a demo site on pythonanywhere.com and to test the initial > >pages I wrote some test code. > [...] > > Hi Nitin, > > I looks like nobody has replied; this probably means that nobody has the > required specialty expertise to help you. (I don't.) > > >Good news, put in 10 hrs, tried so many suggestion given on the net, > >yet non worked. So finally mailing the list. > > Might I suggest that you ask on the django-users mailing list, here: > > https://www.djangoproject.com/community/ > > You want the "Using Django" list there. > > There's a guideline to posting here: > > https://code.djangoproject.com/wiki/UsingTheMailingList > > but your post looks fairly good on the face of it. > > Hoping this helps, > Cameron Simpson > > > > >Objective : display main page with multiple links to other pages. Use > >jQuery plugins, click the link on main page and open other pages. > > > >The following path and pages are posted below. > > > >Thanks > > > >Nitin Chandra > > > >------------------------------------------------------------ > >ERROR > >When I click on 'Table' link on 'home' page, the following error is displayed > > > >http://selftech.pythonanywhere.com/table.html > >Page not found (404) > > > >Request Method:GET > >Request URL:http://selftech.pythonanywhere.com/table.html > > > >Using the URLconf defined in eduinfra.urls, Django tried these URL > >patterns, in this order: > > > >admin/ > >[name='home.html'] > > > >The current path, table.html, didn't match any of these. > > > >You're seeing this error because you have DEBUG = True in your Django > >settings file. Change that to False, and Django will display a > >standard 404 page. > > > >+++++++++++++++++++++++++++++++++++ > > > >/home/selfTech/edu/tech/ > > > > __init__.py > > admin.py > > apps.py > > models.py > > tests.py > > urls.py > > views.py > > > >/home/selfTech/edu/tech/templates/ > > > > base.html > > home.html > > table.html > >******************************************* > >our 'base.html' code below (test code, gets rendered correctly) > > > > > > ************************* > >home.html > > > >{% extends 'base.html' %} > > > >{% block content%} > >Hello TEAM :) > >{% endblock %} > >******************************* > >table.html (gives ERROR 404) > > > > > > > > > > > > > > > >
Name
> > > >********************************************* > >/home/selfTech/edu/tech/ > > > >urls.py > > > >from django.urls import path > >from . import views > > > >urlpatterns = [ > > path('', views.home, name='home'), > > path('table/', views.table, name='table') ## various combo did not work > > # path (' ', views.table, name='table') ## nope > > # path (' ', views.table, name='table.html') # nope > >] > > > >*************************/////////////////////// > >views.py > > > >from django.shortcuts import render > >from django.http import HttpResponse > ># Create your views here. > > > >def home (request): /// correctly renders > > return render (request, "home.html",{}) > > > >def table (request): //// gives error > > return HttpResponse ('

This is a test Page

') > > # Tried the below code, nada > > # return render (request, "table.html",{}) > > > >*************************\\\\\\\\\\\\\\\\\\\\\\\\\ > >_______________________________________________ > >Tutor maillist - Tutor at python.org > >To unsubscribe or change subscription options: > >https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Tue Apr 2 12:25:10 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 Apr 2019 03:25:10 +1100 Subject: [Tutor] Questions about the deprecation of standard library modules In-Reply-To: References: Message-ID: <20190402162509.GY31406@ando.pearwood.info> On Sat, Mar 30, 2019 at 10:13:16PM -0500, boB Stepp wrote: > So my main question is how does one know in which Python version a > deprecated module will be removed? I'm not too concerned about the > imp module, but _do_ want to know how the removal process works for > deprecated standard library modules that I might be interested in. The usual process goes something like this: - after much discussion, the core developers agree to deprecate a module; - in a fit on enthusiasm, the docs are adjusted to note that the module is deprecated; - many years later, somebody remembers the deprecation note in the docs, and wonders whether or not the module should finally be removed; - the module is changed to give a warning when used; - some years after that, somebody asks what's going on with the deprecated module, shouldn't it be removed by now? - and the module is finally removed. (Only half joking.) In practice, it depends on the specific developer, and how good he or she is at coming back to the module and removing it after a "suitable" time. Suitable usually means a minimum of two releases, but it could be as little as one in urgent cases; in *very special circumstances* a module which is a security risk could be removed immediately, but that's rare. Its not unusual for everyone to forget to remove a deprecated feature or module until long after it should have been removed, or to decide that deprecation notice or not, too many people are still using it and so it can't be removed. On the other hand, its also not ususual for modules to be removed promptly. Usually its: - one or two releases silently marked as deprecation pending; - one or two releases marked depreciation (active warning); - and then removed. -- Steven From adeadmarshal at gmail.com Wed Apr 3 04:57:32 2019 From: adeadmarshal at gmail.com (Ali M) Date: Wed, 3 Apr 2019 13:27:32 +0430 Subject: [Tutor] What is the correct way of signalling and slots in PyQt5? Message-ID: The application crashes without errors every time any signal is called on the widgets. how should i signal to my functions? for example these signals make the app crash: self.lineEdit.textChanged.connect(self.edit_input) or self.listWidget.clicked.connect(self.listClicked) here is the full code: from PyQt5 import QtCore, QtGui, QtWidgets import sqlite3 as sqlite import sys class Ui_MainWindow(object): def setupUi(self, MainWindow): super().__init__() self.initUi() def initUi(self): self.centralwidget = QtWidgets.QWidget(MainWindow) self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.textChanged.connect(self.edit_input) self.listWidget = QtWidgets.QListWidget(self.centralwidget) self.listWidget.clicked.connect(self.listClicked) self.textEdit = QtWidgets.QTextEdit(self.centralwidget) MainWindow.setCentralWidget(self.centralwidget) self.db = sqlite.connect(r'C:\Users\deadmarshal\PycharmProjects\Tim\test.db') self.cur = self.db.cursor() self.result = self.cur.execute("SELECT Esperanto FROM Words ORDER BY Esperanto") self.items = self.result.fetchall() for i in self.items: self.listWidget.insertItems(0, i) def listClicked(self): index = self.listWidget.currentRow() results = self.cur.execute("SELECT English FROM Words WHERE Esperanto = ?", (index,)) for row in results: self.textEdit.clear() self.textEdit.insertPlainText(row) def edit_input(self): word_to_esp = {'gx': '?', 'cx': '?', 'hx': '?', 'jx': '?', 'ux': '?', 'sx': '?'} user_input = self.lineEdit.text() user_input = user_input.lower() for i in word_to_esp: if user_input.__contains__(i): a = user_input.replace(i, word_to_esp[i]) return self.lineEdit.set(a) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) From alan.gauld at yahoo.co.uk Wed Apr 3 06:05:40 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 3 Apr 2019 11:05:40 +0100 Subject: [Tutor] What is the correct way of signalling and slots in PyQt5? In-Reply-To: References: Message-ID: On 03/04/2019 09:57, Ali M wrote: > The application crashes without errors every time any signal is called It's very unusual to get no errors at all. How are you running it? Are you using an OS terminal? It may be that you are simply not seeing the errors... However... This list is for the core Python language and standard library and PyQt is not part of that so you may get a better response on the PyQt forums. > here is the full code: I tried running this but (after commenting out the database stuff) only got the last QTtextEdit widget on screen with no other widgets visible. Other than that it ran OK insofar as it didn't crash in any way. A small point: > for i in word_to_esp: > if user_input.__contains__(i): It's considered bad practice to call a dunder method directly. This should be written as if i in user_input: Which is easier to read. 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 From s.molnar at sbcglobal.net Fri Apr 5 14:59:53 2019 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Fri, 05 Apr 2019 14:59:53 -0400 Subject: [Tutor] Problem with Saved File Message-ID: <5CA7A5A9.3040807@sbcglobal.net> I am using Spyder v-3.3.3 on Debian Stretch and have written a script to process data from another application. I am having a problem saving the resulting file [A] as a column vector. The vector is saved by np.savetxt(name_s,[A]). When I open the file in Spyder it is a column vector, but when I import the saved file into QtiPlot it is a line vector. What am I missing in the savetxt statement? Thanks in advance -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528(c) Skype: smolnar1 From alan.gauld at yahoo.co.uk Sat Apr 6 06:42:12 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 6 Apr 2019 11:42:12 +0100 Subject: [Tutor] Problem with Saved File In-Reply-To: <5CA7A5A9.3040807@sbcglobal.net> References: <5CA7A5A9.3040807@sbcglobal.net> Message-ID: On 05/04/2019 19:59, Stephen P. Molnar wrote: > I am using Spyder v-3.3.3 on Debian Stretch and have written a script to > process data from another application. I am having a problem saving the > resulting file [A] as a column vector. > > The vector is saved by np.savetxt(name_s,[A]). > > When I open the file in Spyder it is a column vector, but when I import > the saved file into QtiPlot it is a line vector. Your questions are not really about python but about the specific third party libraries you are using. The best place to get support on those is from their providers. For NumPy its the SciPy community. I'm guessing QtiPlot is part of Qt? By targeting the community who use/support those libraries you are much more likely to find an answer than on a general Python/Programming list like tutor. But there are a few numpy users here so you may get lucky. -- 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 From diana.katz at gmail.com Fri Apr 5 20:01:44 2019 From: diana.katz at gmail.com (Diana Katz) Date: Fri, 5 Apr 2019 20:01:44 -0400 Subject: [Tutor] Questions Message-ID: 1) Can you use python from excel? Or just export to excel? 2) I am trying to see if there's a way using python to automate all of this work that I need to do. I have to collect quarterly segment data for hundreds of public companies and go back at least 12-16 quarters. We use an aggregator like factset and they actually don't have this option available in an automated way. So I'm trying to see if there's a way to build this. Basically, I get my data from sec.gov and they have interactive data - they even have the data in excel (though it's a messy file and hard to read). I attached some of the steps and the data that i'd want to see. Basically i'd want the excel to look like: old to new quarters - going back 12 to 16 quarters (more if possible but not if it will stop the project). Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018... Rows: Sales for segment A Sales for Segment b Sales for SEgment C ?(for as many segments as they have) Earnings for Segment A .Earnings for Segment B Depreciation for Segment A Depreciation for Segment B Depreciation for Segment C... I included where I get the data in the attached document. All the best, Diana Katz From s.molnar at sbcglobal.net Sat Apr 6 13:44:50 2019 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Sat, 06 Apr 2019 13:44:50 -0400 Subject: [Tutor] Problem with Saved File Message-ID: <5CA8E592.4080602@sbcglobal.net> I am using Spyder v-3.3.3 on Debian Stretch and have written a script to process data from another application. I am having a problem saving the resulting file [A] as a column vector. The vector is saved by np.savetxt(name_s,[A]). When I open the file in Spyder it is a column vector, but when I import the saved file into QtiPlot it is a line vector. What am I missing in the savetxt statement? Thanks in advance -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528(c) Skype: smolnar1 From __peter__ at web.de Sun Apr 7 08:07:24 2019 From: __peter__ at web.de (Peter Otten) Date: Sun, 07 Apr 2019 14:07:24 +0200 Subject: [Tutor] Interoperating with Excel, was Re: Questions References: Message-ID: Diana Katz wrote: > 1) Can you use python from excel? Or just export to excel? > 2) I am trying to see if there's a way using python to automate all of > this work that I need to do. I have to collect quarterly segment data for > hundreds of public companies and go back at least 12-16 quarters. We use > an aggregator like factset and they actually don't have this option > available in an automated way. So I'm trying to see if there's a way to > build this. Basically, I get my data from sec.gov and they have > interactive data - they even have the data in excel (though it's a messy > file and hard to read). I attached some of the steps and the data that i'd > want to see. Basically i'd want the excel to look like: > old to new quarters - going back 12 to 16 quarters (more if possible but > not if it will stop the project). > Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018... > Rows: > Sales for segment A > Sales for Segment b > Sales for SEgment C > ?(for as many segments as they have) > > Earnings for Segment A > .Earnings for Segment B > > Depreciation for Segment A > Depreciation for Segment B > Depreciation for Segment C... These look like "pivot tables" which are well supported by Excel. I expect that this is easy to automate with a little bit of Basic. Of course you can build these tables with a Python script if you feel more comfortable in Python. Then either write them into csv files ("comma separated value", supported by the standard library) https://docs.python.org/3/library/csv.html which can be read by Excel -- or use a dedicated library. Google came up with https://xlsxwriter.readthedocs.io/ > I included where I get the data in the attached document. Attachments don't make it to the list. You have to provide a link or (if they're small and text-only) paste your data and scripts into the message body. From george at fischhof.hu Mon Apr 8 13:33:20 2019 From: george at fischhof.hu (George Fischhof) Date: Mon, 8 Apr 2019 19:33:20 +0200 Subject: [Tutor] Questions In-Reply-To: References: Message-ID: Diana Katz ezt ?rta (id?pont: 2019. ?pr. 7., V, 10:01): > 1) Can you use python from excel? Or just export to excel? > 2) I am trying to see if there's a way using python to automate all of this > work that I need to do. I have to collect quarterly segment data for > hundreds of public companies and go back at least 12-16 quarters. We use an > aggregator like factset and they actually don't have this option available > in an automated way. So I'm trying to see if there's a way to build this. > Basically, I get my data from sec.gov and they have interactive data - > they > even have the data in excel (though it's a messy file and hard to read). I > attached some of the steps and the data that i'd want to see. > Basically i'd want the excel to look like: > old to new quarters - going back 12 to 16 quarters (more if possible but > not if it will stop the project). > Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018... > Rows: > Sales for segment A > Sales for Segment b > Sales for SEgment C > ?(for as many segments as they have) > > Earnings for Segment A > .Earnings for Segment B > > Depreciation for Segment A > Depreciation for Segment B > Depreciation for Segment C... > > I included where I get the data in the attached document. > > All the best, > > Diana Katz > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hi Diana, you can write excel addins in Python with this program: https://www.pyxll.com/index.html and you can use excel from Python too. The two ways: - If you do not need the excel process itself (it means that you do not want to recalculate for example, or do any calculation, then perhaps openpyxl would be a great choice for you: https://pypi.org/project/openpyxl/ there are some templates and other stuff as well: https://pypi.org/search/?q=openpyxl - the other way is, when you want to do calculations, then xlwings is your package: https://pypi.org/project/xlwings/ it uses win32com API, there are some edge cases where it does not allow you to access low level com API functions. I faced with this, and created an excel helper over win32com API instead of using xlwings, but I do not think you will need that. (If yes, then code of xlwings can help you BR, __george__ From sjeik_appie at hotmail.com Mon Apr 8 14:38:06 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 8 Apr 2019 18:38:06 +0000 Subject: [Tutor] Interoperating with Excel, was Re: Questions In-Reply-To: Message-ID: On 7 Apr 2019 14:07, Peter Otten <__peter__ at web.de> wrote: Diana Katz wrote: > 1) Can you use python from excel? Or just export to excel? > 2) I am trying to see if there's a way using python to automate all of > this work that I need to do. I have to collect quarterly segment data for > hundreds of public companies and go back at least 12-16 quarters. We use > an aggregator like factset and they actually don't have this option > available in an automated way. So I'm trying to see if there's a way to > build this. Basically, I get my data from sec.gov and they have > interactive data - they even have the data in excel (though it's a messy > file and hard to read). I attached some of the steps and the data that i'd > want to see. Basically i'd want the excel to look like: > old to new quarters - going back 12 to 16 quarters (more if possible but > not if it will stop the project). > Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018... > Rows: > Sales for segment A > Sales for Segment b > Sales for SEgment C > ?(for as many segments as they have) > > Earnings for Segment A > .Earnings for Segment B > > Depreciation for Segment A > Depreciation for Segment B > Depreciation for Segment C... These look like "pivot tables" which are well supported by Excel. I expect that this is easy to automate with a little bit of Basic. Of course you can build these tables with a Python script if you feel more comfortable in Python. Then either write them into csv files ("comma separated value", supported by the standard library) https://docs.python.org/3/library/csv.html which can be read by Excel -- or use a dedicated library. Google came up with https://xlsxwriter.readthedocs.io/ ===>> There's xlrd for xls files, openpyxl for xlsx files. And pandas can also read excel, and you can use groupby, pivottable etc. there: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html. With the win32com library you can also automate Excel (Windows only). Excel is evil, especially if the files are made by humans and come from different sources. From wrw at mac.com Mon Apr 8 21:52:07 2019 From: wrw at mac.com (William Ray Wing) Date: Mon, 8 Apr 2019 21:52:07 -0400 Subject: [Tutor] Questions In-Reply-To: References: Message-ID: <458A71D2-73E7-40CC-AA0C-56925290F4DC@mac.com> Diana, I?m answering you via the Tutor list - please, the accepted protocol is to send all questions and answers to the list so answers can be seen by (and possibly help) others. Having said that, I should have paid more attention to your original question, which is really going to require answers that are beyond the typical Tutor question level, so I?m also forwarding to the main Python list where you should be able to get pointers. But let me ask, how much programming do you know? Python is a full-blown programming language, like Java or C. Have you written programs before that, for example can accept a file name from a user, open that file, and read its contents? If yes, then I apologize, and would point you at: https://medium.freecodecamp.org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe https://towardsdatascience.com/how-to-web-scrape-with-python-in-4-minutes-bc49186a8460 https://realpython.com/python-web-scraping-practical-introduction/ or https://docs.python-guide.org/scenarios/scrape/ The next steps would probably involve loading that scraped data into Pandas: https://pandas.pydata.org/pandas-docs/stable/getting_started/tutorials.html https://data36.com/pandas-tutorial-1-basics-reading-data-files-dataframes-data-selection/ https://www.tutorialspoint.com/python_pandas On the other hand, if your answer to my question is: ?no? - then you should take a look at any of the really vast collection of web sites devoted to Python learning. Note that Python was originally designed to be a language that would be easy for beginners to learn. It still is - I?d claim it is about the easiest - >>> print( "Hello world!" ) Hello world! Those lines were lifted from Alan Gauld?s learn to program web site. Let us know how we can help. Bill > On Apr 8, 2019, at 5:40 PM, Diana Katz wrote: > > Yes - data would need to be scraped from sec.gov website. > I want to be able to pull up segment data from 10-Q filings of individual companies by putting in a ticker (preferably in excel, but an be done elsewhere). Trying to figure out how to even start setting this up. > > Thank you! > > On Sun, Apr 7, 2019 at 8:57 PM William Ray Wing > wrote: > > > > On Apr 5, 2019, at 8:01 PM, Diana Katz > wrote: > > > > 1) Can you use python from excel? Or just export to excel? > > Simple answer: no. Python can read and write excel files through libraries: > > https://www.datacamp.com/community/tutorials/python-excel-tutorial > > > 2) I am trying to see if there's a way using python to automate all of this > > work that I need to do. I have to collect quarterly segment data for > > hundreds of public companies and go back at least 12-16 quarters. We use an > > aggregator like factset and they actually don't have this option available > > in an automated way. So I'm trying to see if there's a way to build this. > > We really need more information to be of any help. Is the starting data coming from a web site? > Python scripts can interact with web sites, ?scrape? data from them or read data from files downloaded in response to the script's interaction with the site. The python library Pandas (named by its originator in the financial field where such data is referred to as ?panel? data) is optimized for manipulating spreadsheet-like tables of data (it includes a pivot operation). > > > Basically, I get my data from sec.gov and they have interactive data - they > > even have the data in excel (though it's a messy file and hard to read). I > > attached some of the steps and the data that i'd want to see. > > Basically i'd want the excel to look like: > > old to new quarters - going back 12 to 16 quarters (more if possible but > > not if it will stop the project). > > Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018... > > Rows: > > Sales for segment A > > Sales for Segment b > > Sales for SEgment C > > ?(for as many segments as they have) > > > > Earnings for Segment A > > .Earnings for Segment B > > > > Depreciation for Segment A > > Depreciation for Segment B > > Depreciation for Segment C... > > > > I included where I get the data in the attached document. > > Since attachments can contain unknown contents, this list drops them. > > Bill > > > > > All the best, > > > > Diana Katz > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > From fatimabttt at gmail.com Tue Apr 16 15:54:30 2019 From: fatimabttt at gmail.com (fatima butt) Date: Tue, 16 Apr 2019 20:54:30 +0100 Subject: [Tutor] Fwd: uploading images in pygame In-Reply-To: References: Message-ID: please i am getting error..i am trying to upload image from openspaceart from internet . I have downloaded the image on my desktop and i am trying to upload this to the pygame. From fatimabttt at gmail.com Tue Apr 16 15:54:52 2019 From: fatimabttt at gmail.com (fatima butt) Date: Tue, 16 Apr 2019 20:54:52 +0100 Subject: [Tutor] Fwd: IDLE Terminal In-Reply-To: References: Message-ID: [image: image.png]please I need help with IDLE teminal..its giving me error. From mats at wichmann.us Tue Apr 16 18:07:33 2019 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 16 Apr 2019 16:07:33 -0600 Subject: [Tutor] Fwd: uploading images in pygame In-Reply-To: References: Message-ID: <6f3eff05-975d-89a7-a406-f90ccb64a502@wichmann.us> On 4/16/19 1:54 PM, fatima butt wrote: > please i am getting error..i am trying to upload image from openspaceart > from internet . I have downloaded the image on my desktop and i am trying > to upload this to the pygame. Please provide us with more information. What are you trying (hint: *code*), what kind of errors are you getting, etc. From alan.gauld at yahoo.co.uk Tue Apr 16 18:33:47 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Apr 2019 23:33:47 +0100 Subject: [Tutor] Fwd: uploading images in pygame In-Reply-To: References: Message-ID: On 16/04/2019 20:54, fatima butt wrote: > please i am getting error.. Hi, we need to be quite specific here about the details because it is not clear exactly what you are trying to do. > i am trying to upload image from openspaceart > from internet . Just to be clear. Uploading means going from your computer onto a server on the internet. So, I'm assuming what you are trying to do is download an image from openspaceart to your computer. Is that correct? Or are you in fact trying to upload an image from your computer to the openspaceart server? There is a big difference between the two. > I have downloaded the image on my desktop So it sounds like you have succeeded in downloading the image from the server and now have a copy on your local computer? Is that correct? > and i am trying > to upload this to the pygame. But pygame is not on a network it is on your computer so you don't need to upload the image, you should just need to access it from within pygame. To help with that we need to know exactly what you are trying to do with the image in pygame. Are you displaying it as a background? Creating sprites? Using it as part of a GUI, maybe on a button? It would really help if you can show us some code. Even if it doesn't work it will help us understand what you are trying to do. Also if you get any error messages send them too. But send it as text, the list does not accept attachments since they pose a security risk. -- 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 From alan.gauld at yahoo.co.uk Tue Apr 16 18:39:15 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 Apr 2019 23:39:15 +0100 Subject: [Tutor] Fwd: IDLE Terminal In-Reply-To: References: Message-ID: On 16/04/2019 20:54, fatima butt wrote: > [image: image.png]please I need help with IDLE teminal..its giving me error. The mail server drops attachments because they are a potential security threat. Please post the error text (cut 'n paste if possible) Also describe what you are trying to do. Which OS you are using and which Python version. The more detail you give s the easier it is to give you the correct answer. -- 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 From toby at tobiah.org Tue Apr 16 18:30:31 2019 From: toby at tobiah.org (Tobiah) Date: Tue, 16 Apr 2019 15:30:31 -0700 Subject: [Tutor] Fwd: IDLE Terminal In-Reply-To: References: Message-ID: On 4/16/19 12:54 PM, fatima butt wrote: > [image: image.png]please I need help with IDLE teminal..its giving me error. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Please copy the error text into your message. The list won't accept attachments. From fatimabttt at gmail.com Wed Apr 17 05:10:04 2019 From: fatimabttt at gmail.com (fatima butt) Date: Wed, 17 Apr 2019 10:10:04 +0100 Subject: [Tutor] Fwd: IDLE Terminal In-Reply-To: References: Message-ID: hi the python version is 3.7.3 computer is acer SWIFT The error I get is following: Traceback (most recent call last): File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 84, in background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() pygame.error: Couldn't open C:\Users\ammah\OneDrive\Documents\project1\ship1.jpg >>> The code that I entered in my Python shell is as following: # Pygame template - skeleton for a new pygame project import pygame import random from os import path img_dir = path.dirname(__file__) WIDTH = 480 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255,255,0) pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((80,70)) self.image.fill(GREEN) self.rect =self.image.get_rect() self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT -10 self.speedx = 0 def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = 5 if keystate[pygame.K_RIGHT]: self.speedx = -5 self.rect.x += self.speedx def shoot(self): bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) class Mob(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface ((40,30)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) def update(self): self.rect.y += self.speedy if self.rect.top > HEIGHT +10: self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((10,20)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.rect.bottom = y self.rect.centerx = x self.speedy = -10 def update(self): self.rect.y += self.speedy if self.rect.bottom<0: self.kill() #Load all game graphics background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() background_rect = background.get_rect() all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() player = Player() all_sprites.add(player) for i in range(8): m = Mob() all_sprites.add(m) mobs.add(m) # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player.shoot() # Update all_sprites.update() #check to see if a bullet hit a mob #check to see if a mob hit the player hits = pygame.sprite.spritecollide(player,mobs,False) if hits: running = False # Draw / render screen.fill(BLACK) screen.blit(background, background_rect) all_sprites.draw(screen) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit() On Tue, 16 Apr 2019 at 23:40, Alan Gauld via Tutor wrote: > On 16/04/2019 20:54, fatima butt wrote: > > [image: image.png]please I need help with IDLE teminal..its giving me > error. > > The mail server drops attachments because they are a potential > security threat. > > Please post the error text (cut 'n paste if possible) > Also describe what you are trying to do. > Which OS you are using and which Python version. > The more detail you give s the easier it is to give > you the correct answer. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From fatimabttt at gmail.com Wed Apr 17 05:13:25 2019 From: fatimabttt at gmail.com (fatima butt) Date: Wed, 17 Apr 2019 10:13:25 +0100 Subject: [Tutor] Fwd: IDLE Terminal In-Reply-To: References: Message-ID: Its IDLE python version 3.7.3 ITs Subprocess Startup Error. IDLE startupprocess didnt make connection.Either IDLE cant start a process or personal firewall software is blocking the connection. the computer i am using is swift acer On Tue, 16 Apr 2019 at 23:40, Alan Gauld via Tutor wrote: > On 16/04/2019 20:54, fatima butt wrote: > > [image: image.png]please I need help with IDLE teminal..its giving me > error. > > The mail server drops attachments because they are a potential > security threat. > > Please post the error text (cut 'n paste if possible) > Also describe what you are trying to do. > Which OS you are using and which Python version. > The more detail you give s the easier it is to give > you the correct answer. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From fatimabttt at gmail.com Wed Apr 17 05:14:15 2019 From: fatimabttt at gmail.com (fatima butt) Date: Wed, 17 Apr 2019 10:14:15 +0100 Subject: [Tutor] Fwd: uploading images in pygame In-Reply-To: References: Message-ID: the python version is 3.7.3 computer is acer SWIFT The error I get is following: Traceback (most recent call last): File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py ", line 84, in background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() pygame.error: Couldn't open C:\Users\ammah\OneDrive\Documents\project1\ship1.jpg >>> The code that I entered in my Python shell is as following: # Pygame template - skeleton for a new pygame project import pygame import random from os import path img_dir = path.dirname(__file__) WIDTH = 480 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255,255,0) pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((80,70)) self.image.fill(GREEN) self.rect =self.image.get_rect() self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT -10 self.speedx = 0 def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = 5 if keystate[pygame.K_RIGHT]: self.speedx = -5 self.rect.x += self.speedx def shoot(self): bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) class Mob(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface ((40,30)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) def update(self): self.rect.y += self.speedy if self.rect.top > HEIGHT +10: self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((10,20)) self.image.fill(YELLOW) self.rect = self.image.get_rect() self.rect.bottom = y self.rect.centerx = x self.speedy = -10 def update(self): self.rect.y += self.speedy if self.rect.bottom<0: self.kill() #Load all game graphics background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() background_rect = background.get_rect() all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() player = Player() all_sprites.add(player) for i in range(8): m = Mob() all_sprites.add(m) mobs.add(m) # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player.shoot() # Update all_sprites.update() #check to see if a bullet hit a mob #check to see if a mob hit the player hits = pygame.sprite.spritecollide(player,mobs,False) if hits: running = False # Draw / render screen.fill(BLACK) screen.blit(background, background_rect) all_sprites.draw(screen) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit() On Tue, 16 Apr 2019 at 23:34, Alan Gauld via Tutor wrote: > On 16/04/2019 20:54, fatima butt wrote: > > please i am getting error.. > > Hi, we need to be quite specific here about the details > because it is not clear exactly what you are trying to do. > > > i am trying to upload image from openspaceart > > from internet . > > Just to be clear. > Uploading means going from your computer onto a server > on the internet. So, I'm assuming what you are trying > to do is download an image from openspaceart to your > computer. Is that correct? > > Or are you in fact trying to upload an image from your > computer to the openspaceart server? > > There is a big difference between the two. > > > I have downloaded the image on my desktop > > So it sounds like you have succeeded in downloading > the image from the server and now have a copy on > your local computer? Is that correct? > > > and i am trying > > to upload this to the pygame. > > But pygame is not on a network it is on your computer > so you don't need to upload the image, you should > just need to access it from within pygame. > > To help with that we need to know exactly what you > are trying to do with the image in pygame. Are you > displaying it as a background? Creating sprites? > Using it as part of a GUI, maybe on a button? > > It would really help if you can show us some code. > Even if it doesn't work it will help us understand > what you are trying to do. > > Also if you get any error messages send them too. > But send it as text, the list does not accept > attachments since they pose a security risk. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Wed Apr 17 07:55:48 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 Apr 2019 13:55:48 +0200 Subject: [Tutor] Fwd: uploading images in pygame References: Message-ID: fatima butt wrote: > the python version is 3.7.3 > computer is acer SWIFT > The error I get is following: > Traceback (most recent call last): > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py > ", line 84, in > background = > pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() > pygame.error: Couldn't open > C:\Users\ammah\OneDrive\Documents\project1\ship1.jpg Are you sure you have an image called 'ship1.jpg' in the "C:\Users\ammah\OneDrive\Documents\project1" folder? Double-check before you take other less likely problems into consideration. If you can see the above file in your filemanager -- does the following script succeed? with open(r"C:\Users\ammah\OneDrive\Documents\project1"), "rb"): pass If it doesn't, what does the traceback show? From alan.gauld at yahoo.co.uk Wed Apr 17 11:11:21 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Apr 2019 16:11:21 +0100 Subject: [Tutor] Fwd: uploading images in pygame In-Reply-To: References: Message-ID: On 17/04/2019 10:14, fatima butt wrote: > the python version is 3.7.3 > computer is acer SWIFT > The error I get is following: > Traceback (most recent call last): > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py > ", line 84, in > background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert() > pygame.error: Couldn't open > C:\Users\ammah\OneDrive\Documents\project1\ship1.jpg In addition to checking that the file exists in that location you should also check the permissions to make sure you are allowed to open it. -- 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 From bhatkarthik1996 at gmail.com Wed Apr 17 11:29:51 2019 From: bhatkarthik1996 at gmail.com (Karthik Bhat) Date: Wed, 17 Apr 2019 20:59:51 +0530 Subject: [Tutor] Off-Topic: Tutor group specific to Java Message-ID: Hello Guys, This is kind of off-topic, but I would really appreciate it if anyone could provide me with a tutor mailing list/group specific to Java. I am a beginner, and it would be really helpful for me. -- Thanks & Regards, Karthik A Bhat From fatimabttt at gmail.com Wed Apr 17 11:44:54 2019 From: fatimabttt at gmail.com (fatima butt) Date: Wed, 17 Apr 2019 16:44:54 +0100 Subject: [Tutor] Help Message-ID: <57BA529C-5BF8-46E6-BB10-F4D7A7EEB7FB@gmail.com> hi Peter, hope you are well.I am getting the following error when i am running the pygame shell script.I am using Acer SWIFT computer.my python version is 3.7.3 and pygame version is pygame 1.9.5 Traceback (most recent call last): File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 166, in draw_text(screen, str(score),18, WIDTH/2,10) File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 28, in draw_text font = pygame.font.Font(font_name, size) pygame.error: font not initialized >>> my code is as following: # Pygame template - skeleton for a new pygame project import pygame import random from os import path img_dir = path.dirname(__file__) WIDTH = 480 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255,255,0) pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() font_name = pygame.font.match_font('arial') def draw_text(surf, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, WHITE) text_rect = text.surface.get_rect() text_rect.midtop =(x,y) surf.blit(text_surface, text_rect) class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(player_img,(50,38)) self.image.set_colorkey(BLACK) self.rect =self.image.get_rect() self.radius = 20 #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT -10 self.speedx = 0 def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = 5 if keystate[pygame.K_RIGHT]: self.speedx = -5 self.rect.x += self.speedx def shoot(self): bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) class Mob(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image_orig = meteor_img self.image_orig = random.choice(meteor_images) self.image_orig.set_colorkey(BLACK) self.image = self.image_orig.copy() self.rect = self.image.get_rect() self.radius = int(self.rect.width *.9 / 2) #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-150,-100) self.speedy=random.randrange(1,8) self.speedx=random.randrange(-3,3) self.rot = 0 self.rot_speed = random.randrange(-8,8) self.last_update = pygame.time.get_ticks() def rotate(self): now = pygame.time.get_ticks() if now - self.last_update > 50: self.last_update = now self.rot = (self.rot + self.rot_speed) % 360 new_image = pygame.transform.rotate(self.image_orig, self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect() self.rect.center = old_center def update(self): self.rotate() self.rect.x += self.speedx self.rect.y += self.speedy if self.rect.top > HEIGHT +10: self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = bullet_img self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.bottom = y self.rect.centerx = x self.speedy = -10 def update(self): self.rect.y += self.speedy if self.rect.bottom<0: self.kill() #Load all game graphics background = pygame.image.load(path.join(img_dir,"purple.png")).convert() background_rect = background.get_rect() player_img = pygame.image.load(path.join(img_dir,"playerShip1_blue.png")).convert() meteor_img = pygame.image.load(path.join(img_dir,"meteorBrown_big1.png")).convert() bullet_img = pygame.image.load(path.join(img_dir,"laserGreen01.png")).convert() meteor_images = [] meteor_list = ['meteorBrown_big1.png','meteorBrown_big2.png','meteorBrown_med1.png', 'meteorBrown_med1.png','meteorBrown_small1.png','meteorBrown_small2.png', 'meteorBrown_big1.png'] for img in meteor_list: meteor_images.append(pygame.image.load(path.join(img_dir,img)).convert()) all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() player = Player() all_sprites.add(player) for i in range(8): m = Mob() all_sprites.add(m) mobs.add(m) score = 0 # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player.shoot() # Update all_sprites.update() #check to see if a bullet hit a mob hits = pygame.sprite.groupcollide(mobs,bullets,True,True) for hit in hits: score += 50 - hit.radius m= Mob() all_sprites.add(m) mobs.add(m) #check to see if a mob hit the player hits = pygame.sprite.spritecollide(player,mobs,False, pygame.sprite.collide_circle) if hits: running = False # Draw / render screen.fill(BLACK) screen.blit(background, background_rect) all_sprites.draw(screen) draw_text(screen, str(score),18, WIDTH/2,10) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit() Sent from my iPhone From fatimabttt at gmail.com Wed Apr 17 11:46:06 2019 From: fatimabttt at gmail.com (fatima butt) Date: Wed, 17 Apr 2019 16:46:06 +0100 Subject: [Tutor] Help Message-ID: <897A5087-AE6B-4419-80E8-F5F9BA0BF610@gmail.com> hi Peter, hope you are well.I am getting the following error when i am running the pygame shell script.I am using Acer SWIFT computer.my python version is 3.7.3 and pygame version is pygame 1.9.5 Traceback (most recent call last): File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 166, in draw_text(screen, str(score),18, WIDTH/2,10) File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 28, in draw_text font = pygame.font.Font(font_name, size) pygame.error: font not initialized >>> my code is as following: # Pygame template - skeleton for a new pygame project import pygame import random from os import path img_dir = path.dirname(__file__) WIDTH = 480 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255,255,0) pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() font_name = pygame.font.match_font('arial') def draw_text(surf, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, WHITE) text_rect = text.surface.get_rect() text_rect.midtop =(x,y) surf.blit(text_surface, text_rect) class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(player_img,(50,38)) self.image.set_colorkey(BLACK) self.rect =self.image.get_rect() self.radius = 20 #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT -10 self.speedx = 0 def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = 5 if keystate[pygame.K_RIGHT]: self.speedx = -5 self.rect.x += self.speedx def shoot(self): bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) class Mob(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image_orig = meteor_img self.image_orig = random.choice(meteor_images) self.image_orig.set_colorkey(BLACK) self.image = self.image_orig.copy() self.rect = self.image.get_rect() self.radius = int(self.rect.width *.9 / 2) #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-150,-100) self.speedy=random.randrange(1,8) self.speedx=random.randrange(-3,3) self.rot = 0 self.rot_speed = random.randrange(-8,8) self.last_update = pygame.time.get_ticks() def rotate(self): now = pygame.time.get_ticks() if now - self.last_update > 50: self.last_update = now self.rot = (self.rot + self.rot_speed) % 360 new_image = pygame.transform.rotate(self.image_orig, self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect() self.rect.center = old_center def update(self): self.rotate() self.rect.x += self.speedx self.rect.y += self.speedy if self.rect.top > HEIGHT +10: self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = bullet_img self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.bottom = y self.rect.centerx = x self.speedy = -10 def update(self): self.rect.y += self.speedy if self.rect.bottom<0: self.kill() #Load all game graphics background = pygame.image.load(path.join(img_dir,"purple.png")).convert() background_rect = background.get_rect() player_img = pygame.image.load(path.join(img_dir,"playerShip1_blue.png")).convert() meteor_img = pygame.image.load(path.join(img_dir,"meteorBrown_big1.png")).convert() bullet_img = pygame.image.load(path.join(img_dir,"laserGreen01.png")).convert() meteor_images = [] meteor_list = ['meteorBrown_big1.png','meteorBrown_big2.png','meteorBrown_med1.png', 'meteorBrown_med1.png','meteorBrown_small1.png','meteorBrown_small2.png', 'meteorBrown_big1.png'] for img in meteor_list: meteor_images.append(pygame.image.load(path.join(img_dir,img)).convert()) all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() player = Player() all_sprites.add(player) for i in range(8): m = Mob() all_sprites.add(m) mobs.add(m) score = 0 # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player.shoot() # Update all_sprites.update() #check to see if a bullet hit a mob hits = pygame.sprite.groupcollide(mobs,bullets,True,True) for hit in hits: score += 50 - hit.radius m= Mob() all_sprites.add(m) mobs.add(m) #check to see if a mob hit the player hits = pygame.sprite.spritecollide(player,mobs,False, pygame.sprite.collide_circle) if hits: running = False # Draw / render screen.fill(BLACK) screen.blit(background, background_rect) all_sprites.draw(screen) draw_text(screen, str(score),18, WIDTH/2,10) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit() Sent from my iPhone From alan.gauld at yahoo.co.uk Wed Apr 17 17:43:08 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 Apr 2019 22:43:08 +0100 Subject: [Tutor] Off-Topic: Tutor group specific to Java In-Reply-To: References: Message-ID: On 17/04/2019 16:29, Karthik Bhat wrote: > This is kind of off-topic, but I would really appreciate it if > anyone could provide me with a tutor mailing list/group specific to Java. > I am a beginner, and it would be really helpful for me. While thee are similar groups for some other languages I've never found anything similar for Java (or C++ for that matter). The best I can suggest is asking on stack Overflow, just be sure to follow the guidelines for asking good questions to avoid flack. -- 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 From __peter__ at web.de Wed Apr 17 17:47:06 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 Apr 2019 23:47:06 +0200 Subject: [Tutor] Help References: <57BA529C-5BF8-46E6-BB10-F4D7A7EEB7FB@gmail.com> Message-ID: fatima butt wrote: > hi Peter, > hope you are well.I am getting the following error when i am running the > pygame shell script.I am using Acer SWIFT computer.my python version is > 3.7.3 and pygame version is pygame 1.9.5 > > Traceback (most recent call last): > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line > 166, in > draw_text(screen, str(score),18, WIDTH/2,10) > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 28, > in draw_text > font = pygame.font.Font(font_name, size) > pygame.error: font not initialized OK, you now have a different script, with a different error. Does that mean you resolved your previous problem? Fine. Regarding the new error I found the following hint https://stackoverflow.com/questions/28517979/pygame-font-error i. e. use pygame.init() by entering the error message into a popular search engine ;) From fatimabttt at gmail.com Thu Apr 18 10:16:50 2019 From: fatimabttt at gmail.com (fatima butt) Date: Thu, 18 Apr 2019 15:16:50 +0100 Subject: [Tutor] pygame help please Message-ID: Hi, I am trying to add lives to my pygame.I am using acer SWIFT computer.I am using 3.7.3 python version and pygame 1.9.5 however the following error is coming: Traceback (most recent call last): File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 304, in draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img) File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 56, in draw_lives img.rect.x = x + 30 * i AttributeError: 'pygame.Surface' object has no attribute 'rect' >>> My code is as following: # Pygame template - skeleton for a new pygame project #Frozen Jam by tgfcoder licensed under CC-BY-3 import pygame import random from os import path img_dir = path.dirname(__file__) snd_dir = path.dirname(__file__) WIDTH = 480 HEIGHT = 600 FPS = 60 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255,255,0) pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() font_name = pygame.font.match_font('arial') def draw_text(surf, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, WHITE) text_rect = text_surface.get_rect() text_rect.midtop =(x,y) surf.blit(text_surface, text_rect) def newmob(): m = Mob() all_sprites.add(m) mobs.add(m) def draw_shield_bar(surf,x,y,pct): if pct < 0: pct = 0 BAR_LENGTH = 100 BAR_HEIGHT = 10 fill = (pct /100)* BAR_LENGTH outline_rect = pygame.Rect(x,y,BAR_LENGTH,BAR_HEIGHT) fill_rect = pygame.Rect(x,y,fill,BAR_HEIGHT) pygame.draw.rect(surf, GREEN, fill_rect) pygame.draw.rect(surf,WHITE,outline_rect, 2) def draw_lives(surf,x,y,lives,img): for i in range(lives): img_rect = img.get_rect() img.rect.x = x + 30 * i img.rect.y = y surf.blit(img, img_rect) class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.transform.scale(player_img,(50,38)) self.image.set_colorkey(BLACK) self.rect =self.image.get_rect() self.radius = 20 #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT -10 self.speedx = 0 self.shield = 100 self.shoot_delay = 250 self.last_shot = pygame.time.get_ticks() self.lives = 3 self.hidden = False self.hide_time = pygame.time.get_ticks() def update(self): # unhide if hidden if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000: self.hidden = False self.rect.centerx = WIDTH/2 self.rect.bottom = HEIGHT - 10 self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 if keystate[pygame.K_SPACE]: self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0 def shoot(self): now = pygame.time.get_ticks() if now - self.last_shot > self.shoot_delay: self.last_shot = now bullet = Bullet(self.rect.centerx, self.rect.top) all_sprites.add(bullet) bullets.add(bullet) shoot_sound.play() def hide(self): #hide the player temporarily self.hidden = True self.hide_timer = pygame.time.get_ticks() self.rect.center = (WIDTH / 2, HEIGHT + 200) class Mob(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image_orig = meteor_img self.image_orig = random.choice(meteor_images) self.image_orig.set_colorkey(BLACK) self.image = self.image_orig.copy() self.rect = self.image.get_rect() self.radius = int(self.rect.width *.9 / 2) #pygame.draw.circle(self.image, RED,self.rect.center, self.radius) self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-150,-100) self.speedy=random.randrange(1,8) self.speedx=random.randrange(-3,3) self.rot = 0 self.rot_speed = random.randrange(-8,8) self.last_update = pygame.time.get_ticks() def rotate(self): now = pygame.time.get_ticks() if now - self.last_update > 50: self.last_update = now self.rot = (self.rot + self.rot_speed) % 360 new_image = pygame.transform.rotate(self.image_orig, self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect() self.rect.center = old_center def update(self): self.rotate() self.rect.x += self.speedx self.rect.y += self.speedy if self.rect.top > HEIGHT +10: self.rect.x =random.randrange(WIDTH-self.rect.width) self.rect.y=random.randrange(-100,-40) self.speedy=random.randrange(1,8) class Bullet(pygame.sprite.Sprite): def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = bullet_img self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.bottom = y self.rect.centerx = x self.speedy = 5 def update(self): self.rect.y += self.speedy if self.rect.top self.frame_rate: self.last_update = now self.frame += 1 if self.frame == len(explosion_anim[self.size]): self.kill() else: center = self.rect.center self.image = explosion_anim[self.size][self.frame] self.rect = self.image.get_rect() self.rect.center = center class Pow(pygame.sprite.Sprite): def __init__(self,center): pygame.sprite.Sprite.__init__(self) self.type = random.choice(['shield','gun']) self.image = powerup_images[self.type] self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.center = center self.speedy = -10 def update(self): self.rect.y += self.speedy if self.rect.bottom<0: self.kill() #Load all game graphics background = pygame.image.load(path.join(img_dir,"purple.png")).convert() background_rect = background.get_rect() player_img = pygame.image.load(path.join(img_dir,"playerShip1_blue.png")).convert() meteor_img = pygame.image.load(path.join(img_dir,"meteorBrown_big1.png")).convert() player_mini_img = pygame.transform.scale(player_img,(25, 19)) player_mini_img.set_colorkey(BLACK) bullet_img = pygame.image.load(path.join(img_dir,"laserGreen01.png")).convert() meteor_images = [] meteor_list = ['meteorBrown_big1.png','meteorBrown_big2.png','meteorBrown_med1.png', 'meteorBrown_med1.png','meteorBrown_small1.png','meteorBrown_small2.png', 'meteorBrown_big1.png'] for img in meteor_list: meteor_images.append(pygame.image.load(path.join(img_dir,img)).convert()) explosion_anim ={} explosion_anim['lg'] = [] explosion_anim['sm'] = [] explosion_anim['player'] = [] for i in range(9): filename = 'Bubble_explo{}.png'.format(i) img = pygame.image.load(path.join(img_dir, filename)).convert() img.set_colorkey(BLACK) img_lg = pygame.transform.scale(img,(75,75)) explosion_anim['lg'].append(img_lg) img_sm = pygame.transform.scale(img,(32,32)) explosion_anim['sm'].append(img_sm) filename = 'E000{}.png'.format(i) img = pygame.image.load(path.join(img_dir, filename)).convert() img.set_colorkey(BLACK) explosion_anim['player'].append(img) powerup_images = {} powerup_images['sheild'] = pygame.image.load(path.join(img_dir, 'shield_gold.png')).convert() powerup_images['gun'] = pygame.image.load(path.join(img_dir, 'bolt_gold.png')).convert() #load all game sounds shoot_sound = pygame.mixer.Sound(path.join(snd_dir,'Laser_Shoot2.WAV')) expl_sounds = [] for snd in ['Explosion11.WAV']: expl_sounds.append(pygame.mixer.Sound(path.join(snd_dir, snd))) player_die_sound = pygame.mixer.Sound pygame.mixer.music.load(path.join(snd_dir,'tgfcoder-FrozenJam-SeamlessLoop.ogg')) pygame.mixer.music.set_volume(0.4) all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() powerups = pygame.sprite.Group() player = Player() all_sprites.add(player) for i in range(8): newmob() score = 0 pygame.mixer.music.play(loops=-1) # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False # Update all_sprites.update() #check to see if a bullet hit a mob hits = pygame.sprite.groupcollide(mobs,bullets,True,True) for hit in hits: score += 50 - hit.radius random.choice(expl_sounds).play() expl = Explosion(hit.rect.center, 'lg') all_sprites.add(expl) if random.random() > 0.9: pow = Pow(hit.rect.center) all_sprites.add(pow) powerups.add(pow) newmob() #check to see if a mob hit the player hits = pygame.sprite.spritecollide(player,mobs,True, pygame.sprite.collide_circle) for hit in hits: player.shield -= hit.radius*2 newmob() if player.shield <= 0: death_explosion = Explosion(player.rect.center, 'player') all_sprites.add(death_explosion) player.hide() player.lives -= 1 player.shield = 100 #if the player died and the explosion has finished playing if player.lives == 0 and not death_explosion.alive(): running = False # Draw / render screen.fill(BLACK) screen.blit(background, background_rect) all_sprites.draw(screen) draw_text(screen, str(score),18, WIDTH/2,10) draw_shield_bar(screen,5,5,player.shield) draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit() From fatimabttt at gmail.com Thu Apr 18 10:26:06 2019 From: fatimabttt at gmail.com (fatima butt) Date: Thu, 18 Apr 2019 15:26:06 +0100 Subject: [Tutor] Help In-Reply-To: References: <57BA529C-5BF8-46E6-BB10-F4D7A7EEB7FB@gmail.com> Message-ID: Hi Peter, Thanks soo much ...Its solved...have a nice day ! On Wed, 17 Apr 2019 at 22:48, Peter Otten <__peter__ at web.de> wrote: > fatima butt wrote: > > > hi Peter, > > hope you are well.I am getting the following error when i am running the > > pygame shell script.I am using Acer SWIFT computer.my python version is > > 3.7.3 and pygame version is pygame 1.9.5 > > > > Traceback (most recent call last): > > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line > > 166, in > > draw_text(screen, str(score),18, WIDTH/2,10) > > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line > 28, > > in draw_text > > font = pygame.font.Font(font_name, size) > > pygame.error: font not initialized > > OK, you now have a different script, with a different error. Does that > mean > you resolved your previous problem? > > Fine. > > Regarding the new error I found the following hint > > https://stackoverflow.com/questions/28517979/pygame-font-error > > i. e. use > > pygame.init() > > by entering the error message into a popular search engine ;) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From iampushkar01 at gmail.com Thu Apr 18 11:18:02 2019 From: iampushkar01 at gmail.com (Pushkar vaity) Date: Thu, 18 Apr 2019 11:18:02 -0400 Subject: [Tutor] Mutable objects as dictionary keys: Accessing , Sorting and Using dictionary Message-ID: Hi, I am using Python 3.7 with anaconda install in PyCharm I am trying to traverse some directories using the os module and grab some tcl procedures and put the proc names in python dictionary as keys. Values are some other info related to the proc. Problem: procs found in the directories can have similar names. But python dict keys cannot be same. So I am using the mutable objects as the key. For Ex: class MyClass: def __init__(self, name): self.name = name def __repr__(self): return repr(self.name) proc_dict[MyClass(proc_name)] = (full_file, proc_args) This has allowed me to have same separate keys in my dictionary. But now, 1. I am not able to access individual dictionary values by specifying only the key name. 2. I am not able to use the 'in' or 'not in' operations on my dictionary successfully. 3. Also, I am not able to use the sorted() function on my dict items. It gives me an error as follows: - for proc_name, (file_name, proc_args) in sorted(proc_dict.items()): - TypeError: '<' not supported between instances of 'MyClass' and 'MyClass' Any ideas on how I can achieve the above points? Thanks in advance! From alan.gauld at yahoo.co.uk Thu Apr 18 20:10:06 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 Apr 2019 01:10:06 +0100 Subject: [Tutor] pygame help please In-Reply-To: References: Message-ID: On 18/04/2019 15:16, fatima butt wrote: > however the following error is coming: > Traceback (most recent call last): > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 304, > in > draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img) > File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 56, > in draw_lives > img.rect.x = x + 30 * i > AttributeError: 'pygame.Surface' object has no attribute 'rect' >>>> > > def draw_lives(surf,x,y,lives,img): > for i in range(lives): > img_rect = img.get_rect() > img.rect.x = x + 30 * i Note that you store the rect in img_rect but then attempt to access it via img.rect. The first form is a variable name. The second is an attribute access. And as the error message says img does not have a rect attribute. You need to use your variable. -- 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 From steve at pearwood.info Fri Apr 19 00:26:09 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 19 Apr 2019 14:26:09 +1000 Subject: [Tutor] Mutable objects as dictionary keys: Accessing , Sorting and Using dictionary In-Reply-To: References: Message-ID: <20190419042608.GJ3010@ando.pearwood.info> On Thu, Apr 18, 2019 at 11:18:02AM -0400, Pushkar vaity wrote: > Hi, > > I am using Python 3.7 with anaconda install in PyCharm > I am trying to traverse some directories using the os module and grab some > tcl procedures and put the proc names in python dictionary as keys. Values > are some other info related to the proc. > > Problem: procs found in the directories can have similar names. But python > dict keys cannot be same. Sorry, can you explain why this is a problem? *Similar* is not enough to cause keys to clash. If you have a proc name "abcd" and a similar name "abcd.1", they count as different keys. Could you use the full pathname? "/some/directory/proc.123" "/another/directory/proc.123" would count as different keys. > So I am using the mutable objects as the key. > > For Ex: class MyClass: > > def __init__(self, name): > self.name = name > > def __repr__(self): > return repr(self.name) I'm not convinced that you actually need this class, but if you do, you ought to define a hash method: def __hash__(self): return hash(self.name) and equality: def __eq__(self, other): if isinstance(other, MyClass): return self.name == other.name return NotImplemented otherwise dict lookups will be by object identity, not value. > proc_dict[MyClass(proc_name)] = (full_file, proc_args) > > This has allowed me to have same separate keys in my dictionary. > But now, > > 1. I am not able to access individual dictionary values by > specifying only the key name. Add the __hash__ and __eq__ methods as above, and then try: values = proc_dict[ MyClass(proc_name) ] > 2. I am not able to use the 'in' or 'not in' operations on my > dictionary successfully. Again, you need __hash__ and __eq__ for them to work correctly. > 3. Also, I am not able to use the sorted() function on my dict > items. It gives me an error as follows: > > > - for proc_name, (file_name, proc_args) in sorted(proc_dict.items()): > - TypeError: '<' not supported between instances of 'MyClass' > and 'MyClass' You need to define a __lt__ method: def __lt__(self, other): if isinstance(other, MyClass): return self.name < other.name return NotImplemented But again, I'm not convinced that you need this class at all. Can you please show us the proc names you get, how you get them, and what you do with them? Using a wrapper class like this is probably the last resort. -- Steven From robertvstepp at gmail.com Fri Apr 19 23:21:59 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 19 Apr 2019 22:21:59 -0500 Subject: [Tutor] Off-Topic: Tutor group specific to Java In-Reply-To: References: Message-ID: On Wed, Apr 17, 2019 at 11:09 AM Karthik Bhat wrote: > > Hello Guys, > This is kind of off-topic, but I would really appreciate it if > anyone could provide me with a tutor mailing list/group specific to Java. > I am a beginner, and it would be really helpful for me. Try Java Ranch: https://javaranch.com/ -- boB From justinthetransporter at gmail.com Sat Apr 20 16:51:11 2019 From: justinthetransporter at gmail.com (Ju Bo) Date: Sat, 20 Apr 2019 16:51:11 -0400 Subject: [Tutor] Trouble with SUM() Message-ID: Hi, I'm trying to write a program that uses a while loop to ask a user for multiple values then use the program to add all the values, however many there may be, then print the sum. I'm having trouble with the sum() function. My code is below: con = "y" while con == "y": AMT = float(input("What is the price of the item? $")) con = input("Would you like to continue? [y/n]") price = float(sum(AMT + AMT)) From cs at cskk.id.au Sat Apr 20 18:27:12 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 21 Apr 2019 08:27:12 +1000 Subject: [Tutor] Trouble with SUM() In-Reply-To: References: Message-ID: <20190420222712.GA42802@cskk.homeip.net> On 20Apr2019 16:51, Ju Bo wrote: >Hi, I'm trying to write a program that uses a while loop to ask a user for >multiple values then use the program to add all the values, however many >there may be, then print the sum. I'm having trouble with the sum() >function. My code is below: > >con = "y" > >while con == "y": > > AMT = float(input("What is the price of the item? $")) > con = input("Would you like to continue? [y/n]") > price = float(sum(AMT + AMT)) We generally would also like to see the output of a run and your explaination of what is wrong with that output, compared to the output you wanted. However, there's an obvious problem in your code above: the value of price is computed entirely from the latest amount, with no reference to any previous amounts. Also, sum() does not take a single numeric vlue, it takes an iterable, such as a list. I suspect what you want to do is append AMT values to a list which is set up as an empty list before the loop. Then _after_ the loop, use sum() to add up all the number in the list and print that. For your reference, here is help(sum): sum(iterable, start=0, /) Return the sum of a 'start' value (default: 0) plus an iterable of numbers When the iterable is empty, return the start value. This function is intended specifically for use with numeric values and may reject non-numeric types. Cheers, Cameron Simpson From steve at pearwood.info Sat Apr 20 20:17:15 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Apr 2019 10:17:15 +1000 Subject: [Tutor] Trouble with SUM() In-Reply-To: References: Message-ID: <20190421001713.GK3010@ando.pearwood.info> On Sat, Apr 20, 2019 at 04:51:11PM -0400, Ju Bo wrote: > con = "y" > while con == "y": > AMT = float(input("What is the price of the item? $")) > con = input("Would you like to continue? [y/n]") > price = float(sum(AMT + AMT)) That's a good first attempt. Rather than set the price to float(sum(AMT + AMT)), which doesn't work, set the price to 0 before the loop starts. Then each time through the loop, set the price to price + AMT: price = price + AMT -- Steven From alan.gauld at yahoo.co.uk Sun Apr 21 02:34:50 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 21 Apr 2019 07:34:50 +0100 Subject: [Tutor] Trouble with SUM() In-Reply-To: References: Message-ID: On 20/04/2019 21:51, Ju Bo wrote: > Hi, I'm trying to write a program that uses a while loop to ask a user for > multiple values then use the program to add all the values, however many > there may be, then print the sum. Your code very nearly does that, it just needs a slight tweak. > I'm having trouble with the sum() > function. You don't really need sum() here but you could use it if you particularly want to. But in that case your understanding of how it works needs adjusting. First lets look at how to do it without sum() > con = "y" > > while con == "y": > AMT = float(input("What is the price of the item? $")) > con = input("Would you like to continue? [y/n]") > price = float(sum(AMT + AMT)) You want to add AMT to price not add AMT to itself. The way to do that is to write price = price + AMT or using a Python shortcut: price += AMT You don't need to convert to float since you already converted the input. adding floats produces a float answer automatically. Incidentally Python convention says to use all uppercase names for constant values (In this case it might be something like the maximum number of items you are allowed to buy). So you should really change AMT to amt. But that is just a style issue. Now, how would we use sum() if we wanted to? sum() works by adding all the values of a sequence such as a list or tuple. So to use sum() we first need to create an empty list of amounts. Each time round the loop we then append AMT to that list. At the end of the loop we can use sum to get the final price. price = sum(amounts) Notice there are no addition operations inside the call to sum(). sum() does all the addition for us internally. -- 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 From ar at zeit.io Mon Apr 22 05:18:15 2019 From: ar at zeit.io (Arup Rakshit) Date: Mon, 22 Apr 2019 14:48:15 +0530 Subject: [Tutor] Question about the object.__del__(self) method Message-ID: <24C725A1-5B8B-4084-93B6-71EB7F5C7783@zeit.io> Consider the below in simple class: class RandomKlass: def __init__(self, x): self.x = x def __del__(self): print("Deleted?") Now when I delete the object created from RandomKlass using `del` operator I see the output ?Deleted??. That means `del` operator calls the __del__ method if available. from python_methods import RandomKlass obj = RandomKlass(10) del obj # Deleted... obj = RandomKlass(10) obj1 = RandomKlass(10) del obj # Deleted... del obj1 # Deleted... Now why then the doc https://docs.python.org/3/reference/datamodel.html#object.__del__ says: > `del x` doesn?t directly call `x.__del__()` ? the former decrements the reference count for `x` by one, and the latter is only called when `x`?s reference count reaches zero. Also what the reference count here means? I know that x can hold only one reference at a time. Thanks, Arup Rakshit ar at zeit.io From alan.gauld at yahoo.co.uk Mon Apr 22 06:05:22 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 Apr 2019 11:05:22 +0100 Subject: [Tutor] Question about the object.__del__(self) method In-Reply-To: <24C725A1-5B8B-4084-93B6-71EB7F5C7783@zeit.io> References: <24C725A1-5B8B-4084-93B6-71EB7F5C7783@zeit.io> Message-ID: On 22/04/2019 10:18, Arup Rakshit wrote: > Consider the below in simple class: > > class RandomKlass: > def __init__(self, x): > self.x = x > > def __del__(self): > print("Deleted?") > > Now when I delete the object created from RandomKlass using `del` operator I see the output ?Deleted??. That means `del` operator calls the __del__ method if available. No it doesn't, it just means that is what seemed to happen in this specific scenario. Now try: >>> from python_methods import RandomKlass >>> o1 = RandomKlass(10) >>> o2 = o1 >>> oblist = [o1,o2] >>> del(o1) >>> del(o2) >>> del(oblist) Deleted... So your __del__() is only called once, after all the references to the instance have been deleted. > Also what the reference count here means? > I know that x can hold only one reference at a time. Remember that variables in Python are just names that refer to objects. So, while the name 'x' can only refer to one object at a time, many other names can also refer to that same object, as in the example above. o1, o2 and oblist[0] and oblist[1] all refer to the same original instance of your class. Each time a new variable references the instance an internal "reference count" is incremented. When a referring name is deleted the reference count is decremented. Once the count reaches zero the instance is deleted and its __del__() method, if it exists, is called. So, only when all the names referring to the instance have been deleted is the __del__() method called. (And it is important to realise that there are cases where __del__() is never called. Do not rely on __del__() for any critical actions - such as saving instance data or shutting down the nuclear reactor.) -- 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 From ar at zeit.io Mon Apr 22 14:07:33 2019 From: ar at zeit.io (Arup Rakshit) Date: Mon, 22 Apr 2019 23:37:33 +0530 Subject: [Tutor] Question about the object.__del__(self) method In-Reply-To: References: <24C725A1-5B8B-4084-93B6-71EB7F5C7783@zeit.io> Message-ID: On 22/04/19 3:35 PM, Alan Gauld via Tutor wrote: > On 22/04/2019 10:18, Arup Rakshit wrote: >> Consider the below in simple class: >> >> class RandomKlass: >> def __init__(self, x): >> self.x = x >> >> def __del__(self): >> print("Deleted?") >> >> Now when I delete the object created from RandomKlass using `del` operator I see the output ?Deleted??. That means `del` operator calls the __del__ method if available. > No it doesn't, it just means that is what seemed to > happen in this specific scenario. > > Now try: > > >>>> from python_methods import RandomKlass >>>> o1 = RandomKlass(10) >>>> o2 = o1 >>>> oblist = [o1,o2] >>>> del(o1) >>>> del(o2) >>>> del(oblist) > Deleted... > > So your __del__() is only called once, after all the > references to the instance have been deleted. > >> Also what the reference count here means? >> I know that x can hold only one reference at a time. > Remember that variables in Python are just names that > refer to objects. So, while the name 'x' can only refer > to one object at a time, many other names can also refer > to that same object, as in the example above. > o1, o2 and oblist[0] and oblist[1] all refer to > the same original instance of your class. > > Each time a new variable references the instance an > internal "reference count" is incremented. When a referring > name is deleted the reference count is decremented. > Once the count reaches zero the instance is deleted > and its __del__() method, if it exists, is called. > So, only when all the names referring to the instance > have been deleted is the __del__() method called. (And it > is important to realise that there are cases where > __del__() is never called. Do not rely on __del__() > for any critical actions - such as saving instance > data or shutting down the nuclear reactor.) > Hello Alan, Nice explanation. I completly got what is going on. Closing the question here. :) -- Thanks, Arup Rakshit From ar at zeit.io Tue Apr 23 02:16:58 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 23 Apr 2019 11:46:58 +0530 Subject: [Tutor] Question on implmenting __getitem__ on custom classes Message-ID: Hi, I wrote below 2 classes to explore how __getitem__(self,k) works in conjuection with list subscriptions. Both code works. Now my questions which way the community encourages more in Python: if isinstance(key, slice): or if type(key) == slice: ? How should I implement this if I follow duck typing, because none of the code currently I wrote using duck typing techiniqe IMO. class MyCustomList: ??? def __init__(self, list = []): ??????? self.list = list ??? def __getitem__(self, key): ??????? if isinstance(key, slice): ??????????? return self.list[key] ??????? else: ??????????? return self.list[key] class MyCustomListV1: ??? def __init__(self, list = []): ??????? self.list = list ??? def __getitem__(self, key): ??????? if type(key) == slice: ??????????? return self.list[key] ??????? else: ??????????? return self.list[key] if __name__ == '__main__': ??? list = MyCustomList(list=[1, 2, 3, 4, 5, 6]) ??? print(list[1:3]) ??? print(list[3]) ??? print("=======\n") ??? list = MyCustomListV1(list=[1, 2, 3, 4, 5, 6]) ??? print(list[1:3]) ??? print(list[3]) If run it, I get the output: [2, 3] 4 ======= [2, 3] 4 -- Thanks, Arup Rakshit From alan.gauld at yahoo.co.uk Tue Apr 23 05:13:41 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 Apr 2019 10:13:41 +0100 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: References: Message-ID: On 23/04/2019 07:16, Arup Rakshit wrote: > which way the community encourages more in Python: if isinstance(key, > slice): or if type(key) == slice: ? I think isinstance is usually preferred although I confess that I usually forget and use type()... But isinstance covers you for subclasses too. > class MyCustomList: > ??? def __init__(self, list = []): > ??????? self.list = list > > ??? def __getitem__(self, key): > ??????? if isinstance(key, slice): > ??????????? return self.list[key] > ??????? else: > ??????????? return self.list[key] The if/else test is meaningless since you return self.list[key] in both cases. You would be better off just calling it directly (and maybe wrapping that in a try block?) def __getitem__(self, key): try: return self.list[key] except ....: Assuming you can think of something useful to put in the except clause. Otherwise just let Python raise the exception and leave the user of the class to worry about what to do. -- 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 From steve at pearwood.info Tue Apr 23 06:10:26 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Apr 2019 20:10:26 +1000 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: References: Message-ID: <20190423101025.GM3010@ando.pearwood.info> On Tue, Apr 23, 2019 at 11:46:58AM +0530, Arup Rakshit wrote: > Hi, > > I wrote below 2 classes to explore how __getitem__(self,k) works in > conjuection with list subscriptions. Both code works. Now my questions > which way the community encourages more in Python: if isinstance(key, > slice): or if type(key) == slice: ? In general, we should normally use `isinstance`, because it works with subclasses. But `slice` can't be subclassed: py> class S(slice): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: type 'slice' is not an acceptable base type so there is no advantage to using `isinstance`. (There is no disadvantage either.) I would use `type(key) is slice` to guarantee[1] that the key is certainly a slice. Why use `is` instead of `==`? The `is` operator will be a tiny bit faster than `==`, but more importantly, you could have a class designed to pretend to be a slice. It isn't easy to do (you would have to write a metaclass, which makes it an advanced technique) but by using `is` we can eliminate even that slim chance. > How should I implement this if I > follow duck typing, because none of the code currently I wrote using > duck typing techiniqe IMO. Why bother? Duck-typing is good for *data* values, but a slice is not a data value, it is a way of specifying a range of indexes. > class MyCustomList: > ??? def __init__(self, list = []): > ??????? self.list = list Watch out here, you have a mutable default value, that probably doesn't work the way you expect. The default value is created ONCE, and then shared, so if you do this: a = MyCustomList() # Use the default list. b = MyCustomList() # Shares the same default list! a.append(1) print(b.list) # prints [1] You probably want: ??? def __init__(self, list=None): if list is None: ??????? list = [] self.list = list > ??? def __getitem__(self, key): > ??????? if isinstance(key, slice): > ??????????? return self.list[key] > ??????? else: > ??????????? return self.list[key] The "isinstance" check is useless, because you do precisely the same thing in both branches. ??? def __getitem__(self, key): ???????? return self.list[key] will do exactly the same, and more efficiently. [1] Not actually a guarantee. -- Steven From ar at zeit.io Tue Apr 23 10:52:56 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 23 Apr 2019 20:22:56 +0530 Subject: [Tutor] What protocol to follow when need to pick either one from __getattr__ and __getattribute__ ? Message-ID: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> I read today 2 methods regarding the customizing the attribute access:__getattr__ and __getattribute__ from https://docs.python.org/3/reference/datamodel.html#special-method-names. What I understood about them is that __getattr__ is called when the requested attribute is not found, and an AttributeError is raised. But later is called everytime unconditionally. I wrote a simple 2 input calculator program, where only 2 operations are permitted Addition and Subtraction. Anything else will cause an not permitted error. class OperationNotPermitted(AttributeError): ??? pass class Calc: ??? def __init__(self, x, y): ??????? self.x = x ??????? self.y = y ??? def __getattr__(self, name): ??????? if name == "sum": ??????????? return self.x + self.y ??????? elif name == 'minus': ??????????? return self.x - self.y ??????? else: ??????????? raise OperationNotPermitted("operation {} is not permitted".format(name)) And here is a run down: from customize_attr_access import * cal = Calc(12, 10) cal.sum 22 cal.minus 2 cal.mul Traceback (most recent call last): ? Python Shell, prompt 5, line 1 ??? # Used internally for debug sandbox under external interpreter ? File "/Users/aruprakshit/python_playground/customize_attr_access.py", line 15, in __getattr__ ??? raise OperationNotPermitted("operation {} is not permitted".format(name)) customize_attr_access.OperationNotPermitted: operation mul is not permitted If I replace __getattr__ with __getattribute__ I found the program works exactly same. Now my questions is in real world when? you have to pick between these 2 pair of special method which protocols a Python dev checks to pick either of the one? Is there any such thing, or either one is fine. Can anyone elaborate this to educate me please? doc said: > This method should either return the (computed) attribute value or raise an AttributeError exception. Another question: My question is that: Can I raise a domain error like OperationNotPermitted when raising instead of AttributeError ? -- Thanks, Arup Rakshit From ar at zeit.io Tue Apr 23 10:57:15 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 23 Apr 2019 20:27:15 +0530 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: <20190423101025.GM3010@ando.pearwood.info> References: <20190423101025.GM3010@ando.pearwood.info> Message-ID: <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> On 23/04/19 3:40 PM, Steven D'Aprano wrote: > Watch out here, you have a mutable default value, that probably doesn't > work the way you expect. The default value is created ONCE, and then > shared, so if you do this: > > a = MyCustomList() # Use the default list. > b = MyCustomList() # Shares the same default list! > a.append(1) > print(b.list) > # prints [1] > > You probably want: > > ??? def __init__(self, list=None): > if list is None: > ??????? list = [] > self.list = list That is really a new thing to me. I didn't know. Why list=None in the parameter list is different than in the body of __init__ method? Can you elaborate this? -- Thanks, Arup Rakshit From mats at wichmann.us Tue Apr 23 12:23:48 2019 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 23 Apr 2019 10:23:48 -0600 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> References: <20190423101025.GM3010@ando.pearwood.info> <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> Message-ID: On 4/23/19 8:57 AM, Arup Rakshit wrote: > On 23/04/19 3:40 PM, Steven D'Aprano wrote: >> Watch out here, you have a mutable default value, that probably doesn't >> work the way you expect. The default value is created ONCE, and then >> shared, so if you do this: >> >> a = MyCustomList()? # Use the default list. >> b = MyCustomList()? # Shares the same default list! >> a.append(1) >> print(b.list) >> # prints [1] >> >> You probably want: >> >> ? ??? def __init__(self, list=None): >> ????????? if list is None: >> ????? ??????? list = [] >> ????????? self.list = list > > That is really a new thing to me. I didn't know. Why list=None in the > parameter list is different than in the body of __init__ method? Can you > elaborate this? > It arises because a function definition is an executable statement, run right then. So a default value in the parameter list is created right then, and then used as needed, and if that default is a mutable (list, dictionary, etc) you get surprises. When an empty list is created in the body, it happens each time the function object is called (it's a method, but it's still just a function object). You can write some experiments to show yourself this in action, it usually makes it sink in more than someone telling you. And don't worry, this is one of the most famous of all Python beginner traps, we've all fallen in it. From steve at pearwood.info Tue Apr 23 12:38:54 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 24 Apr 2019 02:38:54 +1000 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> References: <20190423101025.GM3010@ando.pearwood.info> <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> Message-ID: <20190423163853.GN3010@ando.pearwood.info> On Tue, Apr 23, 2019 at 08:27:15PM +0530, Arup Rakshit wrote: > >You probably want: > > > > ??? def __init__(self, list=None): > > if list is None: > > ??????? list = [] > > self.list = list > > That is really a new thing to me. I didn't know. Why list=None in the > parameter list is different than in the body of __init__ method? Can you > elaborate this? Try running this code and see what happens: def make_default(): print("creating a new list") return [] def function(arg=make_default()): arg.append(1) return arg Now call: function() function() function() and see if you can work out what is happening. Hint: how many new lists are created? when are they created? -- Steven From steve at pearwood.info Tue Apr 23 12:48:54 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 24 Apr 2019 02:48:54 +1000 Subject: [Tutor] What protocol to follow when need to pick either one from __getattr__ and __getattribute__ ? In-Reply-To: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> References: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> Message-ID: <20190423164853.GO3010@ando.pearwood.info> On Tue, Apr 23, 2019 at 08:22:56PM +0530, Arup Rakshit wrote: > I read today 2 methods regarding the customizing the attribute > access:__getattr__ and __getattribute__ from > https://docs.python.org/3/reference/datamodel.html#special-method-names. > What I understood about them is that __getattr__ is called when the > requested attribute is not found, and an AttributeError is raised. But > later is called everytime unconditionally. When you overload __getattribute__, your class will be slow because **every** attribute lookup has to go through your method, instead of only the lookups which fail. And unless you are very careful, you will break things: py> class X(object): ... def __init__(self, x): ... self.x = x ... def __getattribute__(self, name): ... if name is 'x': return self.x ... py> obj = X(999) py> obj.x Traceback (most recent call last): File "", line 1, in File "", line 5, in __getattribute__ [ previous line repeats 332 times ] RecursionError: maximum recursion depth exceeded while calling a Python object Normally, overriding __getattribute__ is considered a very advanced and unusual thing to do. -- Steven From __peter__ at web.de Tue Apr 23 13:04:42 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 23 Apr 2019 19:04:42 +0200 Subject: [Tutor] What protocol to follow when need to pick either one from __getattr__ and __getattribute__ ? References: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> Message-ID: Arup Rakshit wrote: > I read today 2 methods regarding the customizing the attribute > access:__getattr__ and __getattribute__ from > https://docs.python.org/3/reference/datamodel.html#special-method-names. > What I understood about them is that __getattr__ is called when the > requested attribute is not found, and an AttributeError is raised. But > later is called everytime unconditionally. I wrote a simple 2 input > calculator program, where only 2 operations are permitted Addition and > Subtraction. Anything else will cause an not permitted error. > > class OperationNotPermitted(AttributeError): > pass > > class Calc: > def __init__(self, x, y): > self.x = x > self.y = y > > def __getattr__(self, name): > if name == "sum": > return self.x + self.y > elif name == 'minus': > return self.x - self.y > else: > raise OperationNotPermitted("operation {} is not > permitted".format(name)) > > And here is a run down: > > from customize_attr_access import * > cal = Calc(12, 10) > cal.sum > 22 > cal.minus > 2 > cal.mul > Traceback (most recent call last): > Python Shell, prompt 5, line 1 > # Used internally for debug sandbox under external interpreter > File "/Users/aruprakshit/python_playground/customize_attr_access.py", > line 15, in __getattr__ > raise OperationNotPermitted("operation {} is not > permitted".format(name)) > customize_attr_access.OperationNotPermitted: operation mul is not > permitted > > If I replace __getattr__ with __getattribute__ I found the program works > exactly same. No, it doesn't, as __getattribute__ is called for x, and y, too. def __getattribute__(self, name): if name == "sum": return self.x + self.y elif name == 'minus': return self.x - self.y else: raise OperationNotPermitted("operation {} is not permitted".format(name)) Accessing cal.sum will therefore trigger a __getattribute__("x") call which in turn will raise an OperationNotPermitted("operation x ...") exception. > Now my questions is in real world when you have to pick > between these 2 pair of special method which protocols a Python dev > checks to pick either of the one? Is there any such thing, or either one > is fine. Can anyone elaborate this to educate me please? __getattribute__() is rarely needed, __getattr__() is useful when the list of calculated attributes is open-ended and uniform (think proxy). When there is a finite number of calculated attributes the best way to implement them is usually a property: class Calc: def __init__(self, x, y): self.x = x self.y = y @property def sum(self): return self.x + self.y @property def difference(self): return self.x - self.y > > doc said: > > > This method should either return the (computed) attribute value or > raise an AttributeError exception. > > Another question: > > My question is that: Can I raise a domain error like > OperationNotPermitted when raising instead of AttributeError ? You just did ;) I don't think it's a good idea, though. From alan.gauld at yahoo.co.uk Tue Apr 23 18:58:35 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 Apr 2019 23:58:35 +0100 Subject: [Tutor] What protocol to follow when need to pick either one from __getattr__ and __getattribute__ ? In-Reply-To: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> References: <548514f0-4ff9-d2f5-0419-ef8c1c1a7dd8@zeit.io> Message-ID: On 23/04/2019 15:52, Arup Rakshit wrote: > If I replace __getattr__ with __getattribute__ I found the program works > exactly same. Now my questions is in real world when? you have to pick > between these 2 pair of special method which protocols a Python dev > checks to pick either of the one? Is there any such thing, or either one > is fine. Can anyone elaborate this to educate me please? There are some recomendations but I'll leave it to someone better qualified than me to respond. > Another question: > > My question is that: Can I raise a domain error like > OperationNotPermitted when raising instead of AttributeError ? You can raise any kind of error you like however, in general, using the standard error types is better because if your class gets using in another program that already handles the standard error type then your class will get handled too. But, if you raise a non standard error, it may slip through the net. (You can mitigate this by subclassing from the standard error that would normally be raised since catch will catch all subclasses as swell as the stated exception type.) 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 From ar at zeit.io Tue Apr 23 13:39:27 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 23 Apr 2019 23:09:27 +0530 Subject: [Tutor] Question on implmenting __getitem__ on custom classes In-Reply-To: <20190423163853.GN3010@ando.pearwood.info> References: <20190423101025.GM3010@ando.pearwood.info> <6742b925-0d5e-d4c1-7609-31074cc95e69@zeit.io> <20190423163853.GN3010@ando.pearwood.info> Message-ID: <657dcea3-1bab-36f0-568e-fefa791b7023@zeit.io> On 23/04/19 10:08 PM, Steven D'Aprano wrote: > On Tue, Apr 23, 2019 at 08:27:15PM +0530, Arup Rakshit wrote: > >>> You probably want: >>> >>> ??? def __init__(self, list=None): >>> if list is None: >>> ??????? list = [] >>> self.list = list >> That is really a new thing to me. I didn't know. Why list=None in the >> parameter list is different than in the body of __init__ method? Can you >> elaborate this? > Try running this code and see what happens: > > def make_default(): > print("creating a new list") > return [] > > > def function(arg=make_default()): > arg.append(1) > return arg > > > Now call: > > > function() > function() > function() > > > and see if you can work out what is happening. > > Hint: how many new lists are created? when are they created? > > > Hello, You are right. I didn't think on it, as it feels to me very natural as per the experiences from other language like Ruby, JS. It works differently there. A similar Ruby code gives different output than Python does. def make_default() ? print("creating a new list") ? return [] end def function(arg=make_default()) ? arg.push(1) ? return arg end # Now call: p(function()) p(function()) p(function()) And on run: ruby -v sample.rb ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16] creating a new list[1] creating a new list[1] creating a new list[1] But python gives to me: creating a new list [1] [1, 1] [1, 1, 1] Python being an interpreted language works so differently than its other 2 siblings Ruby, JS. Surprised. -- Thanks, Arup Rakshit From bob.griffin76 at gmail.com Tue Apr 23 19:31:37 2019 From: bob.griffin76 at gmail.com (Bob Griffin) Date: Tue, 23 Apr 2019 19:31:37 -0400 Subject: [Tutor] Syntac Error Message-ID: <5cbfa05c.1c69fb81.1d5c6.e952@mx.google.com> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> print("Game Over") Game Over >>> Sent from Mail for Windows 10 Each time I write the program in Python from the book Python Programming, Third Edition, for the absolute beginner. When I run the program and get a syntac error invalid syntac that highlights the first 1 in 3.1.1 above. When I save the program and try to run it from the app on my screen it just blinks and goes back to the app on my desktop. Suggestions please. From alan.gauld at yahoo.co.uk Wed Apr 24 02:31:12 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 24 Apr 2019 07:31:12 +0100 Subject: [Tutor] Syntac Error In-Reply-To: <5cbfa05c.1c69fb81.1d5c6.e952@mx.google.com> References: <5cbfa05c.1c69fb81.1d5c6.e952@mx.google.com> Message-ID: On 24/04/2019 00:31, Bob Griffin wrote: > Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. >>>> print("Game Over") > Game Over >>>> > When I run the program and get a syntac error invalid syntac that highlights the first 1 in 3.1.1 above. OK, That suggests that there is a problem in the way you are trying to run the program. It seems like you are typing the Python 3.1.1 message as part of your code. That's not what you are supposed to do. When you start the Python interactive interpreter by typing py at the Windows command prompt (or using the Windows+"R" dialog) you should get a console window which already contains everything down to the first >>> You do not type that in. The only bit you type in is the print("game over") > When I save the program and try to run it from the app on > my screen it just blinks and goes back to the app on my desktop. I'm not sure which app you are referring to but I'll take a guess at what is happening. The interpreter is starting up, running until the error is displayed then closing again. All so fast that you cannot see it. Try adding a line like input("Hit Enter to quit") at the end of your code. So the whole program should just contain the two lines: print("Game over") input("Hit Enter to quit") Try running that. Hopefully you get the output Game over Hit Enter to quit 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 From ar at zeit.io Wed Apr 24 07:22:33 2019 From: ar at zeit.io (Arup Rakshit) Date: Wed, 24 Apr 2019 16:52:33 +0530 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType Message-ID: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> In the simple code like what are the advantages we get from? Is this so that we can implement more special methods than just __getattr__ and __dir__ in the module level? import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') super().__setattr__(attr, value) sys.modules[__name__].__class__ = VerboseModule I can see a use of its when I play with the module as. Am I thinking it right, or there are other reasons of this setup? import fine_grained_module fine_grained_module.foo = "foo? # Setting foo? repr(fine_grained_module) # 'Verbose fine_grained_module' Thanks, Arup Rakshit ar at zeit.io From alan.gauld at yahoo.co.uk Wed Apr 24 18:22:44 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 24 Apr 2019 23:22:44 +0100 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> Message-ID: On 24/04/2019 12:22, Arup Rakshit wrote: > In the simple code like what are the advantages we get from? I'm not really sure what you are asking about? > Is this so that we can implement more special methods > than just __getattr__ and __dir__ in the module level? Is it just the inheritance from ModuleType? Or is it the assignment of the __class__ attribute? Or something else? > import sys > from types import ModuleType > > class VerboseModule(ModuleType): > def __repr__(self): > return f'Verbose {self.__name__}' > > def __setattr__(self, attr, value): > print(f'Setting {attr}...') > super().__setattr__(attr, value) > sys.modules[__name__].__class__ = VerboseModule There are many things in Python that are not generally used by ordinary Python programmers. In many cases these are things used by the core developers or are intended for advanced meta programming type tasks. As a general rule of thumb any time you see code that accesses dunder attributes (ie those with leading and trailing __ markers) directly it's probably an advanced technique you are unlikely to ever need... But if you want to increase your understanding carry on asking but please be as specific as possible about what it is you want the answer to. -- 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 From ar at zeit.io Thu Apr 25 02:00:28 2019 From: ar at zeit.io (Arup Rakshit) Date: Thu, 25 Apr 2019 11:30:28 +0530 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> Message-ID: On 25/04/19 3:52 AM, Alan Gauld via Tutor wrote: > On 24/04/2019 12:22, Arup Rakshit wrote: >> In the simple code like what are the advantages we get from? > I'm not really sure what you are asking about? Ok. My question is that when people add such a class to the module? What is the goal/intention they met by setting such __class__ attribute of a module. The documentation I am reading just shown this technique but didn't dig into this. They didn't explain why/when should we use this technique, but the how. > >> Is this so that we can implement more special methods >> than just __getattr__ and __dir__ in the module level? > Is it just the inheritance from ModuleType? > > Or is it the assignment of the __class__ attribute? Yes the reason of this assignment. > > Or something else? > >> import sys >> from types import ModuleType >> >> class VerboseModule(ModuleType): >> def __repr__(self): >> return f'Verbose {self.__name__}' >> >> def __setattr__(self, attr, value): >> print(f'Setting {attr}...') >> super().__setattr__(attr, value) >> sys.modules[__name__].__class__ = VerboseModule > There are many things in Python that are not generally used > by ordinary Python programmers. In many cases these are things > used by the core developers or are intended for advanced meta > programming type tasks. As a general rule of thumb any time > you see code that accesses dunder attributes (ie those with > leading and trailing __ markers) directly it's probably an > advanced technique you are unlikely to ever need... That's a good catch. I am currently reading https://docs.python.org/3/reference/index.html source, and it seems like they touched everything possible in Python, without saying which is advance and which is basic part. Very often I get stuck, and coming here to ask and understand it. As the author didn't give the hints anything about what is advanced part, I am assuming these all a Python developer should know irrespective of their years of Python experience and keep readiing. But it feels like a lot in one go. > > But if you want to increase your understanding carry on > asking but please be as specific as possible about what > it is you want the answer to. > -- Thanks, Arup Rakshit From steve at pearwood.info Thu Apr 25 03:51:43 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 25 Apr 2019 17:51:43 +1000 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> Message-ID: <20190425075141.GS3010@ando.pearwood.info> On Thu, Apr 25, 2019 at 11:30:28AM +0530, Arup Rakshit wrote: > On 25/04/19 3:52 AM, Alan Gauld via Tutor wrote: > >On 24/04/2019 12:22, Arup Rakshit wrote: > >>In the simple code like what are the advantages we get from? > >I'm not really sure what you are asking about? > > Ok. My question is that when people add such a class to the module? What > is the goal/intention they met by setting such __class__ attribute of a > module. The documentation I am reading just shown this technique but > didn't dig into this. They didn't explain why/when should we use this > technique, but the how. Can to share which documentation you are reading? I've been using Python since version 1.5, and I've never heard of this technique. Thanks. -- Steven From ar at zeit.io Thu Apr 25 05:22:07 2019 From: ar at zeit.io (Arup Rakshit) Date: Thu, 25 Apr 2019 14:52:07 +0530 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: <20190425075141.GS3010@ando.pearwood.info> References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> Message-ID: On 25/04/19 1:21 PM, Steven D'Aprano wrote: > On Thu, Apr 25, 2019 at 11:30:28AM +0530, Arup Rakshit wrote: >> On 25/04/19 3:52 AM, Alan Gauld via Tutor wrote: >>> On 24/04/2019 12:22, Arup Rakshit wrote: >>>> In the simple code like what are the advantages we get from? >>> I'm not really sure what you are asking about? >> Ok. My question is that when people add such a class to the module? What >> is the goal/intention they met by setting such __class__ attribute of a >> module. The documentation I am reading just shown this technique but >> didn't dig into this. They didn't explain why/when should we use this >> technique, but the how. > Can to share which documentation you are reading? I've been using Python > since version 1.5, and I've never heard of this technique. > > Thanks. > Hello Steven, Here it is: *3.3.2.1. Customizing module attribute access* (https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access) -- Thanks, Arup Rakshit From ar at zeit.io Thu Apr 25 08:10:18 2019 From: ar at zeit.io (Arup Rakshit) Date: Thu, 25 Apr 2019 17:40:18 +0530 Subject: [Tutor] module import from a packager works inconsistent between REPL and command line Message-ID: <8EACB148-8EFB-4B92-9190-89FC04C4340A@zeit.io> I have a small app like this: pizza-shop$ tree . . ??? pizzapy ??? __init__.py ??? menu.py ??? pizza.py 1 directory, 3 files # menu.py from typing import List from pizza import Pizza MENU: List[Pizza] = [ Pizza('Margherita', 30, 10.0), Pizza('Carbonara', 45, 14.99), Pizza('Marinara', 35, 16.99), ] if __name__ == '__main__': print(MENU) # pizza.py import math class Pizza: name: str = '' size: int = 0 price: float = 0 def __init__(self, name: str, size: int, price: float) -> None: self.name = name self.size = size self.price = price def area(self) -> float: return math.pi * math.pow(self.size / 2, 2) def awesomeness(self) -> int: if self.name == 'Carbonara': return 9000 return self.size // int(self.price) * 100 print('pizza.py module name is %s' % __name__) if __name__ == '__main__': print('Carbonara is the most awesome pizza.') Now when I call the menu.py from command like it works as expected. pizza-shop$ python3 pizzapy/menu.py pizza.py module name is pizza [, , ] But it fails in REPL: pizza-shop$ python3 Python 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pizzapy import menu Traceback (most recent call last): File "", line 1, in File "/Users/aruprakshit/python_playground/pizza-shop/pizzapy/menu.py", line 2, in from pizza import Pizza ModuleNotFoundError: No module named 'pizza' >>> What is the reason of this inconsistency ? Thanks, Arup Rakshit ar at zeit.io From Mark.Alderson at cityofglasgowcollege.ac.uk Thu Apr 25 06:54:11 2019 From: Mark.Alderson at cityofglasgowcollege.ac.uk (Mark Alderson) Date: Thu, 25 Apr 2019 11:54:11 +0100 Subject: [Tutor] help with colormode References: <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> Message-ID: <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> hi Ihave a very small program. I want to cycle colours. I cant set the colormode from 1 to 255 tried screen.colormode(255) tells me screen is not defined. the program works without the colormode, but i want to use it. I just change the a and b variable values to generate new art. -------------------------------------code----------------- from turtle import Turtle t = Turtle() t.speed(0) b = 180 a = 35 colormode(255) t.color((55,55,55)) for i in range(200): t.circle(i,a) t.right(b) t.circle(i,a) #input('Press any key to continue...') ------------------------------------------------------------------------- ===========error======================= Traceback (most recent call last): File "H:\python\snowflake.py", line 9, in screen.colormode(255) NameError: name 'screen' is not defined =================================== any help would be appreciated, thanks Mark City of Glasgow College | Scottish Charity Number SCO36198 | VAT Number 59677128 DISCLAIMER :- This email, together with any attachments, may be confidential and the subject of legal privilege. If you are not the intended recipient, please notify the sender of this email immediately, delete this message and note that you are not permitted to print, copy, disclose or use the content in any way. City of Glasgow College does not accept any legal responsibility of liability (including in negligence) for the contents of this communication, nor does it endorse or accept any personal views represented herein. Email communications may be subject to interception by third parties or possible data corruption and City of Glasgow College accepts no responsibility whatsoever for the content of communications altered after transmission from the college network. From __peter__ at web.de Thu Apr 25 14:00:36 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 25 Apr 2019 20:00:36 +0200 Subject: [Tutor] help with colormode References: <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> Message-ID: Mark Alderson wrote: > hi > > Ihave a very small program. I want to cycle colours. I cant set the > colormode from 1 to 255 > > tried screen.colormode(255) > > tells me screen is not defined. the program works without the colormode, > but i want to use it. > > I just change the a and b variable values to generate new art. > > -------------------------------------code----------------- > from turtle import Turtle > t = Turtle() > t.speed(0) > > b = 180 > > a = 35 > > colormode(255) > > t.color((55,55,55)) > for i in range(200): > t.circle(i,a) > t.right(b) > t.circle(i,a) > > > #input('Press any key to continue...') > > ------------------------------------------------------------------------- > > ===========error======================= > Traceback (most recent call last): > File "H:\python\snowflake.py", line 9, in > screen.colormode(255) > NameError: name 'screen' is not defined > =================================== The only name you import is Turtle, so you only have that (and the built- ins). Fortunately you can get the screen from the Turtle, so: from turtle import Turtle ninja = Turtle() ninja.speed(0) screen = ninja.screen screen.colormode(255) b = 180 a = 35 for i in range(200): ninja.color((i + 55, 55, 55)) ninja.circle(i, a) ninja.right(b) ninja.circle(i, a) screen.exitonclick() From rls4jc at gmail.com Thu Apr 25 13:46:31 2019 From: rls4jc at gmail.com (Roger Lea Scherer) Date: Thu, 25 Apr 2019 10:46:31 -0700 Subject: [Tutor] str.replace error Message-ID: I'm working wtih python 3.7 on Windows 10. I'm trying to write some code in order to clean up the data in the csv file. Using spreadsheet language, I want to replace part of a cell ("Basic P1") with an empty string and write it in the comments cell. I thought assigning a variable and replacing the string would be a good idea. Here is my code: import csv with open('somefile') as csvDataFile: csvReader = csv.reader(csvDataFile) for row in range(100): a = "Basic P1" str.replace(a, "") print(next(csvReader)) I get an error: Traceback (most recent call last): File "somefile", line 7, in str.replace(a, "") TypeError: replace() takes at least 2 arguments (1 given) But I think I have 2 arguments: a being the "old" argument as per the documentation, "" being the "new" argument as per the documentation. What am I missing? -- Roger Lea Scherer 623.255.7719 *Strengths:* Input, Strategic, Responsibility, Learner, Ideation From alan.gauld at yahoo.co.uk Thu Apr 25 19:10:41 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Apr 2019 00:10:41 +0100 Subject: [Tutor] help with colormode In-Reply-To: <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> References: <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> <5CC191D3020000F80005F374@riv-0m-gwia-02.cogc.ac.uk> Message-ID: On 25/04/2019 11:54, Mark Alderson wrote: > tried screen.colormode(255) Peter has shown you how to make that work but there is a wee issue below I need to point out. > -------------------------------------code----------------- > from turtle import Turtle > t = Turtle() > t.speed(0) > > b = 180 > a = 35 > > colormode(255) > > t.color((55,55,55)) > for i in range(200): > t.circle(i,a) > t.right(b) > t.circle(i,a) > > > #input('Press any key to continue...') > > ------------------------------------------------------------------------- > > ===========error======================= > Traceback (most recent call last): > File "H:\python\snowflake.py", line 9, in > screen.colormode(255) > NameError: name 'screen' is not defined > =================================== The error message clearly does not reflect the code above. In this case its not too bad but in more complex questions it is very important that you send the actual code that generates the error. Otherwise we wind up just guessing at what might be the problem. Just something for future reference. Regards from Stirling, -- 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 From alan.gauld at yahoo.co.uk Thu Apr 25 20:16:25 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Apr 2019 01:16:25 +0100 Subject: [Tutor] str.replace error In-Reply-To: References: Message-ID: On 25/04/2019 18:46, Roger Lea Scherer wrote: > Here is my code: > > import csv > > with open('somefile') as csvDataFile: > csvReader = csv.reader(csvDataFile) > for row in range(100): what happens if there are more rows in the file than 100? Or even if there are less!? > a = "Basic P1" > str.replace(a, "") where does str come from? You haven't assigned anything to str, this is the first mention. did you mean to do something like for str in csvreader: ? > print(next(csvReader)) > > I get an error: > > Traceback (most recent call last): > File "somefile", line 7, in > str.replace(a, "") > TypeError: replace() takes at least 2 arguments (1 given) I would expect a name error. So I think the code you sent is not the code that generated the error. But without the original code we have no idea what str actually is. If it is some kind of string remember that Python strings are immutable so to get the changed string you need to store the result. Possibly something like: str = str.replace(...) > But I think I have 2 arguments: a being the "old" argument as per the > documentation, "" being the "new" argument as per the documentation. > > What am I missing? We can't say because we can't see the code that produced the error. -- 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 From steve at pearwood.info Fri Apr 26 00:29:01 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Apr 2019 14:29:01 +1000 Subject: [Tutor] str.replace error In-Reply-To: References: Message-ID: <20190426042901.GW3010@ando.pearwood.info> On Thu, Apr 25, 2019 at 10:46:31AM -0700, Roger Lea Scherer wrote: > with open('somefile') as csvDataFile: > csvReader = csv.reader(csvDataFile) > for row in range(100): > a = "Basic P1" > str.replace(a, "") > print(next(csvReader)) I'm not quite sure what you expected this to do, but you've (deliberately? accidently?) run into one of the slightly advanced corners of Python: unbound methods. Apologies in advance, but this may be a bit technical. For a suggestion on how to fix the problem, see right at the end. Normally when you call a string method, you do something like this: mystring = "Something here" newstring = mystring.replace("here", "there") The technical name for the mystring.replace call is a "bound method", as the method is "bound" to a string instance (mystring). The method actually takes *three* arguments: the string to operate on, the substring to be replaced, and the replacement string: (string to operate on) . replace ( substring to be replaced, replacement ) But under the hood, what that turns into is an "unbound method" that calls the method attached to the *class* "str": str.replace(string to operate on, substring to replace, replacement) Alas, when you call an *unbound* method like this: str.replace(a, "") the interpreter may get a bit confused and report a slightly inaccurate error message: > TypeError: replace() takes at least 2 arguments (1 given) For technical reasons, all the counts are off by one. The error message should say: TypeError: replace() takes at least 3 arguments (2 given) but because the same error message gets used for both bound and unbound methods, the counts are off for unbound methods. This is an annoyance, but given that unbound methods are relatively rare, not a large one. So how do you fix your code? (1) First of all, you need a string to operate on. I'm not sure which string that should be -- do you want to check *every* cell? Just the first cell in each row? Every second row? That's up to you to decide. (2) Secondly, strings in Python are "immutable" -- that is to say, they cannot be changed in place. Unlike languages like (say) Pascal, if you want to change the value of a string, you need to re-assign a new string: a = "Hello World!" a.upper() # Doesn't work!!! print(a) # Still gives "Hello World!" Instead you need to say a = a.upper(). (3) Unbound methods are a slightly advanced technique which you don't need here, so you should just use normal method call. Putting those three changes together gives something like this: with open('somefile') as csvDataFile: csvReader = csv.reader(csvDataFile) for row in range(100): # How do you know there are only 100 rows? mystring = "Something here? ~Basic P1~ but I don't know what" print("Before:", mystring) mystring = mystring.replace("Basic P1", "") print("After:", mystring) print(next(csvReader)) Hopefully that's enough to set you on the right track. If not, please feel free to write back to the list and ask more questions! -- Steven From steve at pearwood.info Fri Apr 26 01:28:05 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Apr 2019 15:28:05 +1000 Subject: [Tutor] module import from a packager works inconsistent between REPL and command line In-Reply-To: <8EACB148-8EFB-4B92-9190-89FC04C4340A@zeit.io> References: <8EACB148-8EFB-4B92-9190-89FC04C4340A@zeit.io> Message-ID: <20190426052803.GZ3010@ando.pearwood.info> On Thu, Apr 25, 2019 at 05:40:18PM +0530, Arup Rakshit wrote: > I have a small app like this: Please simplify your code to the minimum needed to demonstrate the problem you are asking about. This bit is excellent: > pizza-shop$ tree . > . > ??? pizzapy > ??? __init__.py > ??? menu.py > ??? pizza.py > > 1 directory, 3 files Nicely shown! But we don't need to see all the gory details of menu.py and especially not of pizza.py, all those methods in pizza.Pizza are irrelevant to the problem. Please read this: http://www.sscce.org/ for a guide. It is written for Java programmers, but it applies to any language. All we need in menu.py is a single line: # menu.py from pizza import Pizza because that's the line that fails. And for pizza.py, all we need is: # pizza.py Pizza = None > Now when I call the menu.py from command like it works as expected. When you ask Python to import a module, it doesn't search the entire hard drive, that could take hours if the drive is big enough. It only looks in the search-path. At runtime, you can see the paths searched like this: import sys print(sys.path) which will show you where Python is looking for modules. When you directly call a module: python path/to/menu.py the directory holding that module is included in the search path, so if you also have path/to/pizza.py the "import pizza" will work. But in the REPL, only the default search path is used. In your case, the fix is, I think, to change menu.py to do this: # was: from pizza import Pizza from pizzapy.pizza import Pizza which I *think* will solve the problem, but I haven't tested it. For what it is worth, importing problems are sometimes annoying to solve. What works as a good design for importing libraries doesn't always make a good design for scripts that you call directly from the command line, and visa versa, so the import system is a bit of a compromise between the two. -- Steven From steve at pearwood.info Fri Apr 26 01:40:57 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Apr 2019 15:40:57 +1000 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> Message-ID: <20190426054057.GA3010@ando.pearwood.info> On Thu, Apr 25, 2019 at 02:52:07PM +0530, Arup Rakshit wrote: > Here it is: *3.3.2.1. Customizing module attribute access* > (https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access) Oh! That's brand new in 3.7, no wonder I didn't know about it. I did see the core developers talking about adding this feature, but I didn't know that they had done so. Your original question was: > In the simple code like what are the advantages we get from? Is this > so that we can implement more special methods than just __getattr__ > and __dir__ in the module level? Yes, that's what the documentation says. I don't see any reason not to believe it. Oh, this is cool! I'm going to enjoy playing with this... py> from types import ModuleType py> class Magic(ModuleType): ... count = 0 ... @property ... def spam(self): ... self.count += 1 ... return ' '.join(['spam']*self.count) ... py> import sys py> sys.modules['__main__'].__class__ = Magic py> import __main__ py> __main__.spam 'spam' py> __main__.spam 'spam spam' py> __main__.spam 'spam spam spam' -- Steven From alan.gauld at yahoo.co.uk Fri Apr 26 04:17:56 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Apr 2019 09:17:56 +0100 Subject: [Tutor] str.replace error In-Reply-To: <20190426042901.GW3010@ando.pearwood.info> References: <20190426042901.GW3010@ando.pearwood.info> Message-ID: On 26/04/2019 05:29, Steven D'Aprano wrote: > (deliberately? accidently?) run into one of the slightly advanced > corners of Python: unbound methods. Ooh, good catch. I completely forgot that the string class' name is str... That's why he didn't get a name error... -- 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 From ar at zeit.io Fri Apr 26 08:48:32 2019 From: ar at zeit.io (Arup Rakshit) Date: Fri, 26 Apr 2019 18:18:32 +0530 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: <20190426054057.GA3010@ando.pearwood.info> References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> Message-ID: <034831ad-9611-8319-59fa-27ce54518382@zeit.io> On 26/04/19 11:10 AM, Steven D'Aprano wrote: > On Thu, Apr 25, 2019 at 02:52:07PM +0530, Arup Rakshit wrote: > >> Here it is: *3.3.2.1. Customizing module attribute access* >> (https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access) > Oh! That's brand new in 3.7, no wonder I didn't know about it. > > I did see the core developers talking about adding this feature, but I > didn't know that they had done so. > > Your original question was: > >> In the simple code like what are the advantages we get from? Is this >> so that we can implement more special methods than just __getattr__ >> and __dir__ in the module level? > Yes, that's what the documentation says. I don't see any reason not to > believe it. Well I believed it, but I was not sure if I understood it correctly. So I was trying to confirm myself by those who understand the language. BTW, one thing I would like to know about this list is that, everytime I send an email I see it the in list after 2 hours approx. Is this for me or everybody? I am just curious. > > Oh, this is cool! I'm going to enjoy playing with this... > > > py> from types import ModuleType > py> class Magic(ModuleType): > ... count = 0 > ... @property > ... def spam(self): > ... self.count += 1 > ... return ' '.join(['spam']*self.count) > ... > py> import sys > py> sys.modules['__main__'].__class__ = Magic > py> import __main__ > py> __main__.spam > 'spam' > py> __main__.spam > 'spam spam' > py> __main__.spam > 'spam spam spam' > > > -- Thanks, Arup Rakshit From alan.gauld at yahoo.co.uk Fri Apr 26 11:28:36 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 26 Apr 2019 16:28:36 +0100 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: <034831ad-9611-8319-59fa-27ce54518382@zeit.io> References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: On 26/04/2019 13:48, Arup Rakshit wrote: > BTW, one thing I would like to know about this list is that, everytime I > send an email I see it the in list after 2 hours approx. Is this for me > or everybody? I am just curious. Just for you! ...And every other new poster :-) It stays on moderation until the volume increases to the point where I get to recognise the email address. Around which point I get motivated to change the moderation setting. I've just done that for you so they should come through a little quicker now. -- 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 From steve at pearwood.info Fri Apr 26 12:14:28 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 27 Apr 2019 02:14:28 +1000 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: <034831ad-9611-8319-59fa-27ce54518382@zeit.io> References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: <20190426161424.GC3010@ando.pearwood.info> On Fri, Apr 26, 2019 at 06:18:32PM +0530, Arup Rakshit wrote: > BTW, one thing I would like to know about this list is that, everytime I > send an email I see it the in list after 2 hours approx. Is this for me > or everybody? I am just curious. Just you, I think. Looking at the headers of your post, I see when you sent the post: Received: from arup.local ([117.194.106.70]) by smtp.gmail.com Fri, 26 Apr 2019 05:48:35 -0700 (PDT) and then a few hops through google, to mail.python.org, and then there seems to be a long delay before mail.python.org sends it back out again. Perhaps you are stuck in the moderator queue? -- Steven From ar at zeit.io Fri Apr 26 13:57:20 2019 From: ar at zeit.io (Arup Rakshit) Date: Fri, 26 Apr 2019 23:27:20 +0530 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: <08407b44-20ef-a634-c2ee-2f598c8cd67d@zeit.io> On 26/04/19 8:58 PM, Alan Gauld via Tutor wrote: > On 26/04/2019 13:48, Arup Rakshit wrote: > >> BTW, one thing I would like to know about this list is that, everytime I >> send an email I see it the in list after 2 hours approx. Is this for me >> or everybody? I am just curious. > Just for you! ...And every other new poster :-) > > It stays on moderation until the volume increases > to the point where I get to recognise the email address. > Around which point I get motivated to change the moderation > setting. I've just done that for you so they should > come through a little quicker now. > Thanks Alan Gauld. As my post was taking hours to come to this, I asked a beginner question to the other list https://mail.python.org/pipermail/python-list/2019-April/740629.html . :) Next time I will be here only. -- Thanks, Arup Rakshit From mats at wichmann.us Fri Apr 26 13:34:51 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 26 Apr 2019 11:34:51 -0600 Subject: [Tutor] str.replace error In-Reply-To: <20190426042901.GW3010@ando.pearwood.info> References: <20190426042901.GW3010@ando.pearwood.info> Message-ID: <0f377ba7-421c-e8f6-f9da-16ace1a88ba4@wichmann.us> On 4/25/19 10:29 PM, Steven D'Aprano wrote: > On Thu, Apr 25, 2019 at 10:46:31AM -0700, Roger Lea Scherer wrote: > >> with open('somefile') as csvDataFile: >> csvReader = csv.reader(csvDataFile) >> for row in range(100): >> a = "Basic P1" >> str.replace(a, "") >> print(next(csvReader)) > > > I'm not quite sure what you expected this to do, but you've > (deliberately? accidently?) run into one of the slightly advanced > corners of Python: unbound methods. accidentally, I believe. notice that the way the Python 3 page on string methods is written, you _could_ read it as you are to use the literal 'str' but in fact you are expected to substitute in the name of your string object. For this specific case: === str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. === So for the example above you're expected to do (without changing the range call, which has been commented on elsewhere: you should just iterate directly over the reader object, that's the way it's designed): for row in range(100): a = "Basic P1" row.replace(a, "") and then hopefully actually do something with the modified 'row', not just go on to the next iteration and throw it away... From ar at zeit.io Fri Apr 26 13:43:01 2019 From: ar at zeit.io (Arup Rakshit) Date: Fri, 26 Apr 2019 23:13:01 +0530 Subject: [Tutor] module import from a packager works inconsistent between REPL and command line In-Reply-To: <20190426052803.GZ3010@ando.pearwood.info> References: <8EACB148-8EFB-4B92-9190-89FC04C4340A@zeit.io> <20190426052803.GZ3010@ando.pearwood.info> Message-ID: <91f92eab-6e0c-5790-a9f3-7eeff167cb1e@zeit.io> On 26/04/19 10:58 AM, Steven D'Aprano wrote: > On Thu, Apr 25, 2019 at 05:40:18PM +0530, Arup Rakshit wrote: > >> I have a small app like this: > Please simplify your code to the minimum needed to demonstrate the > problem you are asking about. This bit is excellent: > >> pizza-shop$ tree . >> . >> ??? pizzapy >> ??? __init__.py >> ??? menu.py >> ??? pizza.py >> >> 1 directory, 3 files > Nicely shown! > > But we don't need to see all the gory details of menu.py and especially > not of pizza.py, all those methods in pizza.Pizza are irrelevant to the > problem. Please read this: > > http://www.sscce.org/ > > for a guide. It is written for Java programmers, but it applies to any > language. > > All we need in menu.py is a single line: > > # menu.py > from pizza import Pizza > > because that's the line that fails. > > And for pizza.py, all we need is: > > # pizza.py > Pizza = None > > >> Now when I call the menu.py from command like it works as expected. > When you ask Python to import a module, it doesn't search the entire > hard drive, that could take hours if the drive is big enough. It only > looks in the search-path. At runtime, you can see the paths searched > like this: > > import sys > print(sys.path) > > which will show you where Python is looking for modules. > > When you directly call a module: > > python path/to/menu.py > > the directory holding that module is included in the search path, so if > you also have path/to/pizza.py the "import pizza" will work. > > But in the REPL, only the default search path is used. > > In your case, the fix is, I think, to change menu.py to do this: > > # was: from pizza import Pizza > from pizzapy.pizza import Pizza > > which I *think* will solve the problem, but I haven't tested it. Thanks for explaining this. I got it, it is terrible confusing for a beginner untill spent some months and use Python daily. Every beginner will be frustated, and probably think to give up the whole Python journey.. haha. Some StackOverflow posts shown modifying sys.path by inserting at position 0, it felt to me as a hacked workaround.. So came here to know how to cook it. :) > > For what it is worth, importing problems are sometimes annoying to > solve. What works as a good design for importing libraries doesn't > always make a good design for scripts that you call directly from the > command line, and visa versa, so the import system is a bit of a > compromise between the two. > > > Yes, yes. -- Thanks, Arup Rakshit From bouncingcats at gmail.com Fri Apr 26 20:22:20 2019 From: bouncingcats at gmail.com (David) Date: Sat, 27 Apr 2019 10:22:20 +1000 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: On Sat, 27 Apr 2019 at 02:14, Alan Gauld via Tutor wrote: > On 26/04/2019 13:48, Arup Rakshit wrote: > > > BTW, one thing I would like to know about this list is that, everytime I > > send an email I see it the in list after 2 hours approx. Is this for me > > or everybody? I am just curious. > > Just for you! ...And every other new poster :-) > > It stays on moderation until the volume increases > to the point where I get to recognise the email address. > Around which point I get motivated to change the moderation > setting. I've just done that for you so they should > come through a little quicker now. I've been reading here for years, and I take this opportunity to give my sincere thanks to everyone who shares their knowledge here. I very occasionally respond to questions here, if I'm confident of what I have to say. I've always wondered why my responses often seemed to be ignored by other participants. Now I know. It's no big deal, but I wonder why you wouldn't approve an address the first time you see that it is being used for legitimate discussion. From alan.gauld at yahoo.co.uk Sat Apr 27 04:40:02 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 Apr 2019 09:40:02 +0100 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: On 27/04/2019 01:22, David wrote: > It's no big deal, but I wonder why you wouldn't approve an address > the first time you see that it is being used for legitimate discussion. Time... It takes about 10 seconds to scan a message and click the button to pass it through (or reject it). It takes about 2-3 minutes to navigate to the admin screens, find the email address and change the moderation settings. So its only if I see the same address multiple times I feel the need to take the time to de-moderate it. If its only an occasional post its easier to just click Accept a few times. -- 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 From akleider at sonic.net Sat Apr 27 11:35:49 2019 From: akleider at sonic.net (Alex Kleider) Date: Sat, 27 Apr 2019 08:35:49 -0700 Subject: [Tutor] changed to: gratitude to mentors In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: <9812ff6bfdf91515617e310de75c369f@sonic.net> On 2019-04-26 17:22, David wrote: > > I've been reading here for years, and I take this opportunity > to give my sincere thanks to everyone who shares their > knowledge here. My experience and my sentiment as well. A hearty "hear, hear." From nathan.delboux at gmail.com Sun Apr 28 01:00:12 2019 From: nathan.delboux at gmail.com (Nathan D'Elboux) Date: Sun, 28 Apr 2019 15:00:12 +1000 Subject: [Tutor] urlGET information Message-ID: Hi all, I am pulling apart a python script called Maltran from github and just looking at the modules its importing. the modules in use and that i can find plenty of info on are. import requests. import sys import re import time import urlGET # No information on urlGET as of yet https://pypi.org/project/urlget/ this is a learning exercise for me so how do more experienced python coders approach modules like this, when you encounter a module that has sparse documentation how do you learn more about the functionality of said module? unpack it and just read the source? or is there like a linux man page equivalent within the wheel that would provide more context? Thanks Nathan From alan.gauld at yahoo.co.uk Sun Apr 28 03:54:11 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 Apr 2019 08:54:11 +0100 Subject: [Tutor] urlGET information In-Reply-To: References: Message-ID: On 28/04/2019 06:00, Nathan D'Elboux wrote: > import urlGET > # No information on urlGET as of yet > > https://pypi.org/project/urlget/ > > this is a learning exercise for me so how do more experienced python coders > approach modules like this, when you encounter a module that has sparse > documentation how do you learn more about the functionality of said module? Assuming a Google search has not thrown up any tutorials then I would: Start with the interactive prompt. >>> dir(urlGET) That gives you a list of names from the module. >>> help(urlGET) Will print docstrings. >>> help(urlGET.name) Will print docstrings for the specified name. >>> urlGET.name will print a value/type message >>> urlGET() will execute a function or give an error telling you how many parameters are expected. >>> usrGET.name(1,2,3) Gets some kind of value back - assuming it was reported as taking 3 args. Or a type error if the args are not suitable. If all that fails then I probably start reading the code... Or looking for a better documented alternative module? -- 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 From steve at pearwood.info Sun Apr 28 05:27:15 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 Apr 2019 19:27:15 +1000 Subject: [Tutor] urlGET information In-Reply-To: References: Message-ID: <20190428092713.GA12773@ando.pearwood.info> On Sun, Apr 28, 2019 at 03:00:12PM +1000, Nathan D'Elboux wrote: > this is a learning exercise for me so how do more experienced python coders > approach modules like this, when you encounter a module that has sparse > documentation how do you learn more about the functionality of said module? > > unpack it and just read the source? If there's no documentation, read the source. If the documentation is inaccurate, incomplete or poor, read the source. If the source code is hard to read and understand, cry. > or is there like a linux man page > equivalent within the wheel that would provide more context? Wheels don't magically create documentation out of thin air. If the library isn't documented, where would it get the documentation from? -- Steven From ar at zeit.io Sun Apr 28 10:30:11 2019 From: ar at zeit.io (Arup Rakshit) Date: Sun, 28 Apr 2019 20:00:11 +0530 Subject: [Tutor] Use case of attaching list of strings as spec to the Mock.mock_add_spec() method? Message-ID: <75A85128-D05B-47CC-B27F-45AA66EBFCEA@zeit.io> Code: from unittest.mock import Mock class C: def get_val(self): print(1) mock = Mock() mock.mock_add_spec(C()) mock.get_val.side_effect = C().get_val print(isinstance(mock, C)) True mock.get_val() 1 In the above code, if I replace `mock.get_val.side_effect = C().get_val` with `mock.get_val.return_value = 1` the code still works same. from unittest.mock import Mock class C: def get_val(self): print(1) mock = Mock() mock.mock_add_spec(C()) mock.get_val.return_value = 1 print(isinstance(mock, C)) True mock.get_val() 1 So question is that: Are there any difference using side_effect and return_value while configuring a return value of a method? From doc of [mock_add_spec()](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.mock_add_spec) it says: > Add a spec to a mock. spec can either be an object or a list of strings. What kind of string it refers here? any random valid strings? Why would some one try to mock a string objects instead of just using that string object itself? How does this part work while attaching a spec to the Mock? I understand the object spec part, not just the list of strings use case. So if anyone can explain this, I will be very helpful. Thanks, Arup Rakshit ar at zeit.io From bouncingcats at gmail.com Sun Apr 28 10:56:35 2019 From: bouncingcats at gmail.com (David) Date: Mon, 29 Apr 2019 00:56:35 +1000 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: On Sun, 28 Apr 2019 at 02:57, Alan Gauld via Tutor wrote: > On 27/04/2019 01:22, David wrote: > > > It's no big deal, but I wonder why you wouldn't approve an address > > the first time you see that it is being used for legitimate discussion. > > Time... > > It takes about 10 seconds to scan a message and click the button > to pass it through (or reject it). It takes about 2-3 minutes to > navigate to the admin screens, find the email address and change the > moderation settings. > > So its only if I see the same address multiple times I feel the > need to take the time to de-moderate it. If its only an occasional > post its easier to just click Accept a few times. Thanks for replying. I'm sorry to hear it's such a headache. It sounds like the list manager interface for that task is dreadful. It should be able to take you straight to the configuration for the user who wrote the message. Or even automatically change the moderation status of the user, after one (or some configurable number) of their messages is approved. I think our timezones are out of sync. It seems to take around a day for my messages to reach the list. I guess I'll just remain as a reader, and only write if I have questions. It's not a problem. I'm grateful for the fact that it's a mailing list, not a forum. Thanks for keeping it spam free. I've written a question occasionally, but I usually manage to answer them myself while writing them, before hitting send, so I didn't trouble you with those :) From alan.gauld at yahoo.co.uk Sun Apr 28 14:23:00 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 Apr 2019 19:23:00 +0100 Subject: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType In-Reply-To: References: <3130CA29-A752-420A-9486-2A7D567D94E1@zeit.io> <20190425075141.GS3010@ando.pearwood.info> <20190426054057.GA3010@ando.pearwood.info> <034831ad-9611-8319-59fa-27ce54518382@zeit.io> Message-ID: On 28/04/2019 15:56, David wrote: > I'm sorry to hear it's such a headache. It sounds like the list > manager interface for that task is dreadful. It should be able to take > you straight to the configuration for the user who wrote the message. Its not that bad but it does involve copying the address then switching screens twice then doing a search for the email address then clicking an option and finally submitting the page. Then navigating back to the moderate queue to handle the next message. > I think our timezones are out of sync. It seems to take around a day > for my messages to reach the list. Quite possibly. I'm based in the UK and read the queue twice a day, so if you miss my early morning scan it will be 8-10 hours later before I look again. Then 12-16 hours before the next morning. Timing is everything! :-) -- 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 From luca.tartufari at live.it Sun Apr 28 12:11:11 2019 From: luca.tartufari at live.it (Dr. Luca T) Date: Sun, 28 Apr 2019 16:11:11 +0000 Subject: [Tutor] Help request ERROR installing beautifulsoup Message-ID: Hi, i'm new in python, i tried to install beautifulsoup but i had back this error: ERROR: Complete output from command python setup.py egg_info: ERROR: Traceback (most recent call last): File "", line 1, in File "C:\Users\Luca\AppData\Local\Temp\pip-install-u6zd808q\beautifulsoup\setup.py", line 22 print "Unit tests have failed!" ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Unit tests have failed!")? ---------------------------------------- ERROR: Command "python setup.py egg_info" failed with error code 1 in C:\Users\Luca\AppData\Local\Temp\pip-install-u6zd808q\beautifulsoup\ I use windows 10, python 3.7.3 and a minipc with 32-bit technology inside, can you help me telling me where i'm wrong please? ? Thanks. Luca Tartufari From matthew.polack at htlc.vic.edu.au Sun Apr 28 04:49:20 2019 From: matthew.polack at htlc.vic.edu.au (Matthew Polack) Date: Sun, 28 Apr 2019 18:49:20 +1000 Subject: [Tutor] What does 'Bind Return Key' do? Message-ID: Hi, We're learning Python with PySimpleGUi and have used this example program... https://github.com/PySimpleGUI/PySimpleGUI/blob/master/ProgrammingClassExamples/Win10%20versions/1d%20PSG%20(named%20input%20keys%20and%20catch%20errors).py There is a mystery command that says: [sg.ReadButton('Submit', bind_return_key = False)]] If I change this 'Bind_Return_Key' value from False to True...or in fact delete it entirely, it appears to make do difference whatsoever to the functioning of the program that I can see... Could someone explain what the purpose of this could be? I'm guessing it has to have a reason to be there! Thanks! -- **Disclaimer: *Whilst every attempt has been made to ensure that material contained in this email is free from computer viruses or other defects, the attached files are provided, and may only be used, on the basis that the user assumes all responsibility for use of the material transmitted. This email is intended only for the use of the individual or entity named above and may contain information that is confidential and privileged. If you are not the intended recipient, please note that any dissemination, distribution or copying of this email is strictly prohibited. If you have received this email in error, please notify us immediately by return email or telephone +61 3 5382 2529**?and destroy the original message.* From nathan-tech at hotmail.com Sun Apr 28 20:26:49 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Mon, 29 Apr 2019 00:26:49 +0000 Subject: [Tutor] feedparser in python Message-ID: Hello everyone, Most recently, I have started work using feedparser. I noticed, almost straight away, it's a? bit slow. For instance: ??? url="http://feeds.bbci.co.uk/news/rss.xml" ??? f1=feedparser.parse(url) On some feeds, this can take a few seconds, on the talk python to me feed, it takes almost 10! This, obviously, is not ideal when running a program which checks for updates every once in a while. Talk about slooooow! I tried using etag, and modified, but none of the feeds seem to ever have them! Similarly, this doesn't seem to work: ??? f2=feedparser.parse(url, f.headers["date"]) What am I doing wrong? Any help appreciated. A greatly frustrated Nate From __peter__ at web.de Mon Apr 29 03:23:49 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 Apr 2019 09:23:49 +0200 Subject: [Tutor] What does 'Bind Return Key' do? References: Message-ID: Matthew Polack wrote: > Hi, > > We're learning Python with PySimpleGUi and have used this example > program... > > https://github.com/PySimpleGUI/PySimpleGUI/blob/master/ProgrammingClassExamples/Win10%20versions/1d%20PSG%20(named%20input%20keys%20and%20catch%20errors).py > > > There is a mystery command that says: > > [sg.ReadButton('Submit', bind_return_key = False)]] > > If I change this 'Bind_Return_Key' value from False to True...or in fact > delete it entirely, it appears to make do difference whatsoever to the > functioning of the program that I can see... > > Could someone explain what the purpose of this could be? I'm guessing it > has to have a reason to be there! With bind_return_key=True hitting the key has the same effect as clicking on the [Submit] button. From __peter__ at web.de Mon Apr 29 03:31:54 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 Apr 2019 09:31:54 +0200 Subject: [Tutor] Help request ERROR installing beautifulsoup References: Message-ID: Dr. Luca T wrote: > Hi, > i'm new in python, i tried to install beautifulsoup but i had back this > error: > > ERROR: Complete output from command python setup.py egg_info: > ERROR: Traceback (most recent call last): > File "", line 1, in > File > "C:\Users\Luca\AppData\Local\Temp\pip-install- u6zd808q\beautifulsoup\setup.py", > line 22 > print "Unit tests have failed!" > ^ > SyntaxError: Missing parentheses in call to 'print'. Did you mean > print("Unit tests have failed!")? > ---------------------------------------- > ERROR: Command "python setup.py egg_info" failed with error code 1 in > C:\Users\Luca\AppData\Local\Temp\pip-install-u6zd808q\beautifulsoup\ > > I use windows 10, python 3.7.3 and a minipc with 32-bit technology inside, > can you help me telling me where i'm wrong please? ? Try installing bs4 instead of beautifulsoup to get a version that works with Python 3. From alan.gauld at yahoo.co.uk Mon Apr 29 03:43:10 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Apr 2019 08:43:10 +0100 Subject: [Tutor] feedparser in python In-Reply-To: References: Message-ID: On 29/04/2019 01:26, nathan tech wrote: > Most recently, I have started work using feedparser. I've never heard of it let alone used it so there may be another forum where you can get specific answers. But let me ask... > I noticed, almost straight away, it's a? bit slow. How do you measure slow? What speed did you expect? What other xml parsers have you tried? etree for example? How much faster was it compared to feedparser? > For instance: > > ??? url="http://feeds.bbci.co.uk/news/rss.xml" > ??? f1=feedparser.parse(url) So it looks like the parer is doing more than just parsing it is also fetching the data over the net. How long does that take? Could it be a slow connection or server? Can you try parsing a feed stored on the local machine to eliminate that portion of the work? Is it much faster? If so its the network causing the issue. > On some feeds, this can take a few seconds, on the talk python to me > feed, it takes almost 10! How big is the feed? If its many megabytes then 10s might not be too bad. > This, obviously, is not ideal when running a program which checks for > updates every once in a while. Talk about slooooow! When I talk about "sloooooow" I'm thinking about something that takes a long time relative to how long it would take me manually. If downloading and parsing these feeds by hand would take you 5 minutes per feed then 10s is quite fast... But if parsing by hand takes 30s then 10s would indeed be sloooow. > Similarly, this doesn't seem to work: > > ??? f2=feedparser.parse(url, f.headers["date"]) define "doesn't work"? Does the PC crash? Does it not fetch the data? Does it fail to find "date"? Do you get an error message - if so what? > What am I doing wrong? No idea, you haven't given us enough information. -- 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 From alan.gauld at yahoo.co.uk Mon Apr 29 03:44:31 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 Apr 2019 08:44:31 +0100 Subject: [Tutor] Help request ERROR installing beautifulsoup In-Reply-To: References: Message-ID: On 28/04/2019 17:11, Dr. Luca T wrote: ^ > SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Unit tests have failed!")? > ---------------------------------------- > I use windows 10, python 3.7.3 ... The problem is you are running python 2 code using python 3. You need to find a python 3 version of your package. -- 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 From mats at wichmann.us Mon Apr 29 11:13:55 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 29 Apr 2019 09:13:55 -0600 Subject: [Tutor] Help request ERROR installing beautifulsoup In-Reply-To: References: Message-ID: On 4/29/19 1:44 AM, Alan Gauld via Tutor wrote: > On 28/04/2019 17:11, Dr. Luca T wrote: > ^ >> SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Unit tests have failed!")? >> ---------------------------------------- > >> I use windows 10, python 3.7.3 ... > > The problem is you are running python 2 code using python 3. > You need to find a python 3 version of your package. > You definitely want beautifulsoup4, not just because the version 3 one doesn't work with Python 3, but also because the authors tell you not to use it: "This package is OBSOLETE. It has been replaced by the beautifulsoup4 package. You should use Beautiful Soup 4 for all new projects." By the way, if you are using pip to install, it is recommended to use it through the Python interpreter you intend to use. Since you're on Windows, hopefully you've let your install set up the Python Launcher and then it would look like: py -m pip install beautifulsoup4 From mats at wichmann.us Mon Apr 29 11:27:33 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 29 Apr 2019 09:27:33 -0600 Subject: [Tutor] feedparser in python In-Reply-To: References: Message-ID: <0331adb9-dfba-fbec-d2c2-c51e3a02b92c@wichmann.us> On 4/28/19 6:26 PM, nathan tech wrote: > Hello everyone, > > Most recently, I have started work using feedparser. > > I noticed, almost straight away, it's a? bit slow. > > For instance: > > ??? url="http://feeds.bbci.co.uk/news/rss.xml" > > ??? f1=feedparser.parse(url) > > > On some feeds, this can take a few seconds, on the talk python to me > feed, it takes almost 10! > > This, obviously, is not ideal when running a program which checks for > updates every once in a while. Talk about slooooow! > > > I tried using etag, and modified, but none of the feeds seem to ever > have them! > > Similarly, this doesn't seem to work: > > ??? f2=feedparser.parse(url, f.headers["date"]) > > What am I doing wrong? > > Any help appreciated. > > A greatly frustrated Nate This is just an aside... programs which depend on fetching things from the Web are candidates for various advanced programming techniques. One is to not write synchronously, where you ask for some data, process the data, and present results, as if everything happened instantly. Instead various techniques like using callbacks, or multiple threads, or multiprocessing, or Python's asynchronous facilities can be employed. Documentation for several of those techniques use web communications as their examples :) In other words, think of writing your code so other work can happen while waiting for a particular response (for example firing off requests to other feeds), and thing of how your update checking can happen in the background so the data is there when you want to look at it. Another is when you write your unit tests, mock the responses from the internet servers so your tests don't suffer the same delays as interactive use of the program will see. From ar at zeit.io Mon Apr 29 13:55:51 2019 From: ar at zeit.io (Arup Rakshit) Date: Mon, 29 Apr 2019 23:25:51 +0530 Subject: [Tutor] self.name is calling the __set__ method of another class Message-ID: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> Hi, In the following code, class attributes name and email is set to the instances of NonBlank. class NonBlank: def __init__(self, storage_name): self.storage_name = storage_name def __set__(self, instance, value): if not isinstance(value, str): raise TypeError("%r must be of type 'str'" % self.storage_name) elif len(value) == 0: raise ValueError("%r must not be empty" % self.storage_name) instance.__dict__[self.storage_name] = value class Customer: name = NonBlank('name') email = NonBlank('email') def __init__(self, name, email, fidelity=0): self.name = name self.email = email self.fidelity = fidelity def full_email(self): return '{0} <{1}>'.format(self.name, self.email) if __name__ == '__main__': cus = Customer('Arup', 99) Running this code throws an error: Traceback (most recent call last): File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 25, in cus = Customer('Arup', 99) File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ self.email = email File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 7, in __set__ raise TypeError("%r must be of type 'str'" % self.storage_name) TypeError: 'email' must be of type 'str' Process terminated with an exit code of 1 Now I am not getting how the __set__() method from NonBlank is being called inside the __init__() method. Looks like some magic is going on under the hood. Can anyone please explain this how self.name and self.email assignment is called the __set__ from NonBlank? What is the name of this concept? Thanks, Arup Rakshit ar at zeit.io From steve at pearwood.info Mon Apr 29 14:10:39 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 30 Apr 2019 04:10:39 +1000 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> Message-ID: <20190429181038.GE12773@ando.pearwood.info> On Mon, Apr 29, 2019 at 11:25:51PM +0530, Arup Rakshit wrote: > Now I am not getting how the __set__() method from NonBlank is being > called inside the __init__() method. Looks like some magic is going on > under the hood. Can anyone please explain this how self.name and > self.email assignment is called the __set__ from NonBlank? What is the > name of this concept? I haven't read your code in detail, but it sounds like the Descriptor protocol. Descriptors are used "under the hood" by Python to implement methods, classmethod, staticmethod and property, among others, and are considered an advanced technique (only slightly less advanced than metaclasses). https://docs.python.org/3/howto/descriptor.html If you are *not* intentionally trying to write a custom descriptor, you should not use a __set__ method. (Perhaps you meant __setitem__?) In general, you should treat all dunder (Double UNDERscore) methods as private to Python, and only implement those that you need. Don't use them for your own purposes. -- Steven From ar at zeit.io Mon Apr 29 15:17:02 2019 From: ar at zeit.io (Arup Rakshit) Date: Tue, 30 Apr 2019 00:47:02 +0530 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <20190429181038.GE12773@ando.pearwood.info> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <20190429181038.GE12773@ando.pearwood.info> Message-ID: <5f7b0791-7342-4fab-de2a-da00c69868a2@zeit.io> On 29/04/19 11:40 PM, Steven D'Aprano wrote: > On Mon, Apr 29, 2019 at 11:25:51PM +0530, Arup Rakshit wrote: > >> Now I am not getting how the __set__() method from NonBlank is being >> called inside the __init__() method. Looks like some magic is going on >> under the hood. Can anyone please explain this how self.name and >> self.email assignment is called the __set__ from NonBlank? What is the >> name of this concept? > > I haven't read your code in detail, but it sounds like the Descriptor > protocol. Descriptors are used "under the hood" by Python to implement > methods, classmethod, staticmethod and property, among others, and are > considered an advanced technique (only slightly less advanced than > metaclasses). > > https://docs.python.org/3/howto/descriptor.html > > If you are *not* intentionally trying to write a custom descriptor, you > should not use a __set__ method. (Perhaps you meant __setitem__?) > > In general, you should treat all dunder (Double UNDERscore) methods as > private to Python, and only implement those that you need. Don't use > them for your own purposes. > > I really didn't write that code by myself. The day I'll you will not see me here everyday :) . I was watching a PyCon video https://youtu.be/81S01c9zytE?t=8172 where the author used this code. But his explanation is not clear to me. The main problem is that the guy who was recorded it far away from the projector, so what speaker were showing there is not clear. So thought to ask here as usual. Because I felt so lost with this trick. -- Thanks, Arup Rakshit From steve at pearwood.info Mon Apr 29 19:41:44 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 30 Apr 2019 09:41:44 +1000 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <5f7b0791-7342-4fab-de2a-da00c69868a2@zeit.io> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <20190429181038.GE12773@ando.pearwood.info> <5f7b0791-7342-4fab-de2a-da00c69868a2@zeit.io> Message-ID: <20190429234143.GG12773@ando.pearwood.info> On Tue, Apr 30, 2019 at 12:47:02AM +0530, Arup Rakshit wrote: > I really didn't write that code by myself. The day I'll you will not see > me here everyday :) . I was watching a PyCon video > https://youtu.be/81S01c9zytE?t=8172 where the author used this code. But > his explanation is not clear to me. The main problem is that the guy who > was recorded it far away from the projector, so what speaker were > showing there is not clear. So thought to ask here as usual. Because I > felt so lost with this trick. Okay, the short, SIMPLIFIED (and therefore inaccurate) summary of descriptors: Descriptors are the "magic" used by Python whenever it does an attribute lookup. When you do any sort of attribute lookup or assignment: x = spam.eggs spam.eggs = value Python looks at spam and spam's class for an attribute called "eggs", and if that attribute is an object with a __set__ or __get__ method, it calls that method: x = spam.eggs => x = spam.eggs.__get__() spam.eggs = value => spam.eggs.__set__(value) For the gory details of what *precisely* happens, see the Howto Guide: https://docs.python.org/3/howto/descriptor.html Python has a few common descriptors built in: - ordinary methods - classmethod - staticmethod - property Apart from staticmethod, they're all pretty common in code. But writing your own custom descriptors is fairly rare. I've only done it once, in 25+ years of using Python. -- Steven From mike_barnett at hotmail.com Mon Apr 29 15:55:14 2019 From: mike_barnett at hotmail.com (Mike Barnett) Date: Mon, 29 Apr 2019 19:55:14 +0000 Subject: [Tutor] What does 'Bind Return Key' do? In-Reply-To: References: Message-ID: You'll find it discussed in a couple of places in the PySimpleGUI documentation. For example, this was written about it under the Button Element section of the docs: ---------------------------------------------- The ENTER key : The ENTER key is an important part of data entry for windows. There's a long tradition of the enter key being used to quickly submit windows. PySimpleGUI implements this by tying the ENTER key to the first button that closes or reads a window. The Enter Key can be "bound" to a particular button so that when the key is pressed, it causes the window to return as if the button was clicked. This is done using the bind_return_key parameter in the button calls. If there are more than 1 button on a window, the FIRST button that is of type Close window or Read window is used. First is determined by scanning the window, top to bottom and left to right. ---------------------------------------------- When questions like this arise on PySimpleGUI, I recommend pulling up the docs and pressing Control-F to do a search. @mike -----Original Message----- From: Matthew Polack Sent: Sunday, April 28, 2019 4:49 AM To: tutor at python.org Subject: [Tutor] What does 'Bind Return Key' do? Hi, We're learning Python with PySimpleGUi and have used this example program... https://github.com/PySimpleGUI/PySimpleGUI/blob/master/ProgrammingClassExamples/Win10%20versions/1d%20PSG%20(named%20input%20keys%20and%20catch%20errors).py There is a mystery command that says: [sg.ReadButton('Submit', bind_return_key = False)]] If I change this 'Bind_Return_Key' value from False to True...or in fact delete it entirely, it appears to make do difference whatsoever to the functioning of the program that I can see... Could someone explain what the purpose of this could be? I'm guessing it has to have a reason to be there! Thanks! -- **Disclaimer: *Whilst every attempt has been made to ensure that material contained in this email is free from computer viruses or other defects, the attached files are provided, and may only be used, on the basis that the user assumes all responsibility for use of the material transmitted. This email is intended only for the use of the individual or entity named above and may contain information that is confidential and privileged. If you are not the intended recipient, please note that any dissemination, distribution or copying of this email is strictly prohibited. If you have received this email in error, please notify us immediately by return email or telephone +61 3 5382 2529**?and destroy the original message.* From nathan-tech at hotmail.com Mon Apr 29 19:23:36 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Mon, 29 Apr 2019 23:23:36 +0000 Subject: [Tutor] feedparser in python In-Reply-To: References: Message-ID: Hi there, After reading your email, I did some further investigation, I first did this test: ??? import feedparser ??? import time ??? def tim(url): ???? k=time.time() ???? feedparser.parse(url) ???? return time.time()-k The results were as follows: ??? tim( a url): 2.9 seconds ??? tim(the downoaded file(: 1.8 seconds That tells me that roughly 1.1 seconds is network related, fair enough. I admit, I've not tried etree, as I was specificly working with RSS feeds but will aim to do so soon. My specific problem here is that, everywhere? when I look on how to check to see if an rss feed has been updated, without downloading the entire thing again, they all say use ETAG and Modified, but my feeds never, have them. I've tried feeds from several sources, and none have them in the http header. To that end, that is why I mentioned in the previous email about .date, because that seemed the most likely, but even that failed. My goal is thus: 1, download a feed to the computer. 2. Occasionally, check the website to see if the donloaded feed is out of date if it is, redownload it. Ultimately, I want to complete step 2 without downloading the *entire* feed again, though. I did think about using threading for this, for example: program loads, user sees downloaded feed data only, in the background, the program checks for updates on each feed, and the user may see them gradually start to update. This would work, in that execution would not fail at any time, but it seems... clunky, to me I suppose? And rather data jheavy for the end user, especially if, as you suggest, a feed is 10 MB in size. Furthering to that, how many threads is safe? Should I have my main thread, plus 4 feeds updating at once? 5? 20000? Any help is appreciated. Thanks Nate On 29/04/2019 08:43, Alan Gauld via Tutor wrote: > On 29/04/2019 01:26, nathan tech wrote: > >> Most recently, I have started work using feedparser. > I've never heard of it let alone used it so there may > be another forum where you can get specific answers. > But let me ask... > >> I noticed, almost straight away, it's a? bit slow. > How do you measure slow? What speed did you expect? > What other xml parsers have you tried? etree for example? > How much faster was it compared to feedparser? > >> For instance: >> >> ??? url="http://feeds.bbci.co.uk/news/rss.xml" >> ??? f1=feedparser.parse(url) > So it looks like the parer is doing more than just > parsing it is also fetching the data over the net. > How long does that take? Could it be a slow connection > or server? > > Can you try parsing a feed stored on the local > machine to eliminate that portion of the work? > Is it much faster? If so its the network causing the issue. > >> On some feeds, this can take a few seconds, on the talk python to me >> feed, it takes almost 10! > How big is the feed? If its many megabytes then 10s might > not be too bad. > >> This, obviously, is not ideal when running a program which checks for >> updates every once in a while. Talk about slooooow! > When I talk about "sloooooow" I'm thinking about > something that takes a long time relative to how long > it would take me manually. If downloading and parsing > these feeds by hand would take you 5 minutes per feed > then 10s is quite fast... > > But if parsing by hand takes 30s then 10s would indeed > be sloooow. > >> Similarly, this doesn't seem to work: >> >> ??? f2=feedparser.parse(url, f.headers["date"]) > define "doesn't work"? > Does the PC crash? Does it not fetch the data? > Does it fail to find "date"? > Do you get an error message - if so what? > >> What am I doing wrong? > No idea, you haven't given us enough information. > From cs at cskk.id.au Mon Apr 29 21:03:36 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 30 Apr 2019 11:03:36 +1000 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> Message-ID: <20190430010336.GA19282@cskk.homeip.net> On 29Apr2019 23:25, Arup Rakshit wrote: >In the following code, class attributes name and email is set to the >instances of NonBlank. > >class NonBlank: > def __init__(self, storage_name): > self.storage_name = storage_name > > def __set__(self, instance, value): > if not isinstance(value, str): > raise TypeError("%r must be of type 'str'" % self.storage_name) > elif len(value) == 0: > raise ValueError("%r must not be empty" % self.storage_name) > instance.__dict__[self.storage_name] = value > >class Customer: > name = NonBlank('name') > email = NonBlank('email') > > def __init__(self, name, email, fidelity=0): > self.name = name > self.email = email > self.fidelity = fidelity > > def full_email(self): > return '{0} <{1}>'.format(self.name, self.email) > >if __name__ == '__main__': > cus = Customer('Arup', 99) > >Running this code throws an error: > >Traceback (most recent call last): > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 25, in > cus = Customer('Arup', 99) > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ > self.email = email > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 7, in __set__ > raise TypeError("%r must be of type 'str'" % self.storage_name) >TypeError: 'email' must be of type 'str' >Process terminated with an exit code of 1 > >Now I am not getting how the __set__() method from NonBlank is being >called inside the __init__() method. Looks like some magic is going on >under the hood. Can anyone please explain this how self.name and >self.email assignment is called the __set__ from NonBlank? What is the >name of this concept? As Steven has mentioned, it looks like NonBlank is a descriptor, which defined here: https://docs.python.org/3/glossary.html#term-descriptor So NonBlank has a __set__ method. The above text says: When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup. Normally, using a.b to get, set or delete an attribute looks up the object named b in the class dictionary for a, but if b is a descriptor, the respective descriptor method gets called. So when your new Customer object runs its __init_ method and goes: self.name = name Since Customer.name is a descriptor, this effectively calls: NonBlank.__set__(self, name) which in turn does some type and value checking and then directly modifies self.__dict__ to effect the assignment. So yes, some magic is occurring - that is what the Python dunder methods are for: to provide the mechanism for particular magic actions. Descriptors are rarely used directly, however the @property decorator is quite ommon, where you define methodlike functions which look like attributes. Untested example: class Foo: def __init__(self): self.timestamp = time.time() @property def age(self): return time.time() - self.timestamp which you'd access directly as: foo = Foo() print("age =", foo.age) @property arranges this using descriptors: in the example above it arranges that the class "age" attribute is a descriptor with a __get__ method. Cheers, Cameron Simpson From PyTutor at DancesWithMice.info Mon Apr 29 21:04:39 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Tue, 30 Apr 2019 13:04:39 +1200 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> Message-ID: <9c29679c-6354-f802-5e1b-85a3d16bb3d4@DancesWithMice.info> Hi Arup, On 30/04/19 5:55 AM, Arup Rakshit wrote: > class NonBlank: > def __init__(self, storage_name): > self.storage_name = storage_name > > def __set__(self, instance, value): > if not isinstance(value, str): > raise TypeError("%r must be of type 'str'" % self.storage_name) > elif len(value) == 0: > raise ValueError("%r must not be empty" % self.storage_name) > instance.__dict__[self.storage_name] = value > > class Customer: > name = NonBlank('name') > email = NonBlank('email') > > def __init__(self, name, email, fidelity=0): > self.name = name > self.email = email > self.fidelity = fidelity > > def full_email(self): > return '{0} <{1}>'.format(self.name, self.email) > > if __name__ == '__main__': > cus = Customer('Arup', 99) > > Running this code throws an error: > > Traceback (most recent call last): > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 25, in > cus = Customer('Arup', 99) > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ > self.email = email > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 7, in __set__ > raise TypeError("%r must be of type 'str'" % self.storage_name) > TypeError: 'email' must be of type 'str' > Process terminated with an exit code of 1 > > Now I am not getting how the __set__() method from NonBlank is being called inside the __init__() method. Looks like some magic is going on under the hood. Can anyone please explain this how self.name and self.email assignment is called the __set__ from NonBlank? What is the name of this concept? Use the tools provided - follow the Traceback and interpret each step:- > cus = Customer('Arup', 99) means: instantiate a Customer object, which takes us to > def __init__(self, name, email, fidelity=0): where: - name is set to a string: 'Arup' - email is set to an *integer*: 99, and - fidelity is set to become another integer with a value of 0 (in the first instance) Ignoring name, we arrive at > self.email = email which *appears to be* the creation of an integer(!) within the cus Customer instance. However (the "magic") when the module was loaded into the Python interpreter self.email has already been defined as: > email = NonBlank('email') which means that: > def __init__(self, storage_name): > self.storage_name = storage_name made it (past tense!) an instance of the NonBlank object with a storage_name of email. (and with a __set__ method). So, returning to the Trace, specifically: > File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ > self.email = email what now happens is that the self.email instance of a NonBlank object receives the value passed-in as email (ie 99), and invokes the method: > def __set__(self, instance, value): In due course, we find that 99 is not an acceptable value: > if not isinstance(value, str): > raise TypeError("%r must be of type 'str'" % self.storage_name) and thus: > TypeError: 'email' must be of type 'str' > Process terminated with an exit code of 1 Crash! Of course it is a 'toy example' - when you could plug two 'is it a string' checks straight into Customer, why not keep-it-simple and do just that? - without the added abstraction on top of an abstraction! However, the author is illustrating a useful tool - should you find a situation where the 'checks' are much more involved or complex. (NB in addition to, not an alternative to, the discussions Steven has offered) Given previous conversations, I'm not surprised that you were mystified. The fact that I had to read it twice, and that the above explanation is NOT a 'straight line', indicates that there is probably a better (more simple) approach - and one which is MUCH more likely to be understood by our Python programming colleagues (possibly including our 'future selves'!) As Steven explained, this is a complex environment where only those with a good understanding of the meta abstractions would even want to play (IMHO). Perhaps you would be better served by actually writing some Python applications, and with such experience under-your-belt, adding these 'advanced knowledge' ideas at some later time, if/when needed?) Assuming use of a recent version of Python, you may like to solve this specific problem the same way you might in other programming languages: <<< typing ? Support for type hints New in version 3.5. Note The typing module has been included in the standard library on a provisional basis. New features might be added and API may change even between minor releases if deemed necessary by the core developers. This module supports type hints as specified by PEP 484 and PEP 526. The most fundamental support consists of the types Any, Union, Tuple, Callable, TypeVar, and Generic. For full specification please see PEP 484. For a simplified introduction to type hints see PEP 483. The function below takes and returns a string and is annotated as follows: def greeting(name: str) -> str: return 'Hello ' + name In the function greeting, the argument name is expected to be of type str and the return type str. Subtypes are accepted as arguments. >>> https://docs.python.org/3/library/typing.html -- Regards =dn From alan.gauld at yahoo.co.uk Tue Apr 30 03:47:42 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 30 Apr 2019 08:47:42 +0100 Subject: [Tutor] feedparser in python In-Reply-To: References: Message-ID: On 30/04/2019 00:23, nathan tech wrote: > The results were as follows: > > ??? tim( a url): 2.9 seconds > > ??? tim(the downoaded file(: 1.8 seconds > > > That tells me that roughly 1.1 seconds is network related, fair enough. Or about 30% of the time. Since the network element will increase as data size increases as will the parse time it may be a near linear relationship. Only more extensive tests would tell. > entire thing again, they all say use ETAG and Modified, but my feeds > never, have them. > > I've tried feeds from several sources, and none have them in the http > header. Have you looked at the headers to see what they do have? > To that end, that is why I mentioned in the previous email about .date, > because that seemed the most likely, but even that failed. Again you tell us that something failed. But don't say how it failed. Do you mean that date did not exist? Why did you think it would if you had already inspected the headers? Can you share some actual code that you used to check these fields? And sow us the actual headers you are reading? > 1, download a feed to the computer. > > 2. Occasionally, check the website to see if the donloaded feed is out > of date if it is, redownload it. Seems a good plan. You just need to identify when changes occur. Even better would be if the sites provided a web API to access the data programmatically, but of course few sites do that... > I did think about using threading for this, for example: > user sees downloaded feed data only, in the background, the program > checks for updates on each feed, and the user may see them gradually > start to update. > > This would work, in that execution would not fail at any time, but it > seems... clunky, to me I suppose? And rather data jheavy for the end > user, especially if, as you suggest, a feed is 10 MB in size. Only data heavy if you download everything. If you only do the headers and you only have a relatively few feeds its a good scheme. As an alternative is there anything in the feed body that identifies its creation date? Could you change your parsing mechanism to parse the data as it arrives and stop if the date/time has not changed? That minimises the download data. > Furthering to that, how many threads is safe? You have a lot of I/O going on so you could run quite a few threads without blocking issues. How many feeds do you watch? Logic would say have one thread per feed. But how real time does this really need to be? Would it be terrible if updates were, say 1 minute late? If that's the case a single threaded solution may be fine. (and much simpler) I'd certainly focus on a single threaded solution initially. Get it working first then think about performance tuning. -- 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 From alan.gauld at yahoo.co.uk Tue Apr 30 11:48:40 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 30 Apr 2019 16:48:40 +0100 Subject: [Tutor] Fwd: Re: feedparser in python In-Reply-To: References: Message-ID: Sharing with the list, comments later. Busy right now. -------- Forwarded Message -------- Subject: Re: [Tutor] feedparser in python Date: Tue, 30 Apr 2019 14:14:35 +0000 From: nathan tech To: Alan Gauld Hi Alan, Thanks for your emails. I considered what you said, and came up with a couple of possibilities, listed below. Before that, I wanted to clarify what I meant when I said "not working." I kept meaning to do it, and kept forgetting. According to the docs: ?????? f=feedparser.parse(url) Will download a feed, parse it into xml, and return a dict of that feed, which it does. There are obviously some special things going on with tha,t because it allows, for instance, f.entries[0].title, rather than f["entries"][0]["title"]. Anyway. The docs then say that feedparser will have elements of etag and modified, which you can then pass in an update, like so: ?????? newfeed=feedparser.parse(url, etag=f.etag, modified=f.modified) To that end, it would check the headers, and if the feed was not updated, set newfeed.status to 304. Which is great, accept... My feeds never have a .etag or a .modified anywhere. Even f.get("etag") returns None. which while I could pass it that way, would mean the feed gets downloaded over and over and over again. In an example of an rss feed of size 10 MB, that's 240 MB a day, and by 3 days you're over a GIG. To that end, when I said not working, I meant, nothing I parsed in place of f.etag and or f.modified seemed to work in that it juts downloaded the entire feed agai Now, onto some solutions: I considered what you said and realised actually, logic says all we need to know is: is file on local hard drive older than file on web server, right? Which lead me briefly to, would os.path.getfilemtime work? Probably not, but I am curious if there are alternatives to thhat. In any case, finally I thought, what about f.entries This is a list of entries in an rss feed. Even without an update key, which they usually have: ?? date=f.entries[0].updated = "Fri, August 20th 2009" We could simply do: ?? if(downlaoded_first_entry==f.entries[0]): ???????? # feed is jup to date, so quit. This is where I got stuck. urllib2.urlopen() from my calculations, seems to download the file, then open it? Is that correct, or is that wrong? I wrote up this function below: ?????? import urllib2 ?????? import time ?????? url="https://www.bigfinish.com/podcasts.rss" ?????? start_time=time.time() ?????? j=urllib2.urlopen(url) ?????? j.close() # lets not be messy ?????? print time.time()-start_time That came out at 0.8 seconds. perhaps that is just network connectivity? but if we remember back to the tests run with the tim function, the difference in time there was around 1.1 seconds. The similarities were.. worrying is all. If urllib2.urlopen doesn't download the file, and merely opens a link up, as it were, then great. My theory here is to: open the web file, discard any data up to "" until "" is reached, save the data to a list. Covnert that list using an xml parser into a dictionary, and then compare either updated, title, or the whole thing. If one of them says, this isn't right, download the feed. If they match, the feed on local drive is up to date. To be fair, I could clean this up further, and simply have: until or is reached save to a list, but that's a refinement for later. I'm looking forward to hear your thoughts on this. I picked up python myself over the course of a year, so am not quite used to having back and forth like these yet. Especially not with someone who knows what they're talking about. :) Thanks Nate On 30/04/2019 08:47, Alan Gauld via Tutor wrote: > On 30/04/2019 00:23, nathan tech wrote: > >> The results were as follows: >> >> ?????? tim( a url): 2.9 seconds >> >> ?????? tim(the downoaded file(: 1.8 seconds >> >> >> That tells me that roughly 1.1 seconds is network related, fair enough. > Or about 30% of the time. > Since the network element will increase as data > size increases as will the parse time it may be > a near linear relationship. Only more extensive > tests would tell. > >> entire thing again, they all say use ETAG and Modified, but my feeds >> never, have them. >> >> I've tried feeds from several sources, and none have them in the http >> header. > Have you looked at the headers to see what they do have? > >> To that end, that is why I mentioned in the previous email about .date, >> because that seemed the most likely, but even that failed. > Again you tell us that something failed. But don't say > how it failed. Do you mean that date did not exist? > Why did you think it would if you had already inspected > the headers? > > Can you share some actual code that you used to check > these fields? And sow us the actual headers you are > reading? > >> 1, download a feed to the computer. >> >> 2. Occasionally, check the website to see if the donloaded feed is out >> of date if it is, redownload it. > Seems a good plan. You just need to identify when changes occur. > > Even better would be if the sites provided a web API to access > the data programmatically, but of course few sites do that... > > >> I did think about using threading for this, for example: >> user sees downloaded feed data only, in the background, the program >> checks for updates on each feed, and the user may see them gradually >> start to update. >> >> This would work, in that execution would not fail at any time, but it >> seems... clunky, to me I suppose? And rather data jheavy for the end >> user, especially if, as you suggest, a feed is 10 MB in size. > Only data heavy if you download everything. If you only do the > headers and you only have a relatively few feeds its a good scheme. > > As an alternative is there anything in the feed body that identifies > its creation date? Could you change your parsing mechanism to > parse the data as it arrives and stop if the date/time has not > changed? That minimises the download data. > >> Furthering to that, how many threads is safe? > You have a lot of I/O going on so you could run quite a few threads > without blocking issues. How many feeds do you watch? Logic > would say have one thread per feed. > > But how real time does this really need to be? Would it be > terrible if updates were, say 1 minute late? If that's the case > a single threaded solution may be fine. (and much simpler) > I'd certainly focus on a single threaded solution initially. Get it > working first then think about performance tuning. > >