Re: [Tutor] code review
On 12/06/14 00:38, Alan Gauld wrote: > > HTH Thanks Alan and Lukáš for your very helpful comments. I will attempt to revise the script in light of them and will revert if I hit any brick walls :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem reading large files in binary mode
Try reading the file in chunks instead: CHUNKSIZE = 2**20 hash = hashlib.md5() while True: chunk = f.read(CHUNKSIZE) if not chunk: break hash.update(chunk) hashvalue = hash.hexdigest() Thank you peter for the above valubale reply. but shouldn't read() by itself work because i have enough memory to load it or should it be a bug. thank you george --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MIT - Free Python course started today
oh cool. I actually started taking this here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/ But to have access to a forum and other people taking it at the same time will be wonderful. thank you for posting this. Signing up right now :). Deb in WA, USA > -Original Message- > From: mari...@gmx.com > Sent: Wed, 11 Jun 2014 16:42:01 -0600 > To: tutor@python.org > Subject: [Tutor] MIT - Free Python course started today > > Sort of off topic, (don't kill me) but then again very useful for > beginners! > > MIT started free Python course today: > > https://www.edx.org/course/mitx/mitx-6-00-1x-introduction-computer-1841 > > Unfortunately they are using Python 2.7 :-( > You can even get a certificate if you finish with high enough score > (will cost you something for certificate) > > Anyway, even if partially off topic I think it may be beneficiary for > some beginners here. I'm already going through the lessons :-) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! Check it out at http://www.inbox.com/marineaquarium ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] global variables/constants versus volatile variables/constants
Hi All! Hope everyone is well. In my code there are many dictionaries and lists which are used in various functions. Is it better/pythonic/efficient to have these inside the function itself or declared at the beginning of the program in which case they will be global? They are all read only. I understand that global constants and variable have memory allocated to them but when declared inside a function are created on the fly, used and discarded. Please enlighten me further on this and correct me if i'm wrong. Thank you for your time and may you be well. -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ** ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A couple newbie questions about Python
> -Original Message- > From: breamore...@yahoo.co.uk > Sent: Thu, 12 Jun 2014 09:18:16 +0100 > To: tutor@python.org > Subject: Re: [Tutor] A couple newbie questions about Python > > Please don't top post, it makes following a long thread extremely > difficult, thanks. > sorry. The other python list wants you to, so I just assumed... Deb in WA, USA She says as she almost top posts again :P... FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! Check it out at http://www.inbox.com/marineaquarium ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'module' object has no attribute 'start'
Thnx for all the suggestions...Now i have made amendments to my program and its working now...but there is a new problem nowI am now calling a python script generated by GNU Radio Companion(for those who know about GRC will understand my question better)...when i press the execute button i get the following error..Both files are as attached.Is there a different way to call a script generated by GRC?? Form: -> Error translating value: "<__main__.MyFrame; proxy of >" bad operand type for abs(): 'MyFrame' Enter a float with optional scale suffix. E.g., 100.1M Form: -> Error translating value: "<__main__.MyFrame; proxy of >" unsupported operand type(s) for -: 'MyFrame' and 'float' Value should be within slider range Using Volk machine: avx_64_mmx_orc Traceback (most recent call last): File "wxgui.py", line 26, in Execute self.aNewFrame = uhd_fft.uhd_fft(self) File "/home/ali/Desktop/WXGUI/uhd_fft.py", line 155, in __init__ size=((-1, 400)), File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fftsink_gl.py", line 126, in __init__ persist_alpha=persist_alpha, File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fft_window.py", line 304, in __init__ self.update_grid() File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fft_window.py", line 401, in update_grid baseband_freq - sample_rate/2.0, TypeError: unsupported operand type(s) for -: 'MyFrame' and 'float' Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 102, in widget.Bind(EVT_DATA, lambda x: self._update(x.data)) File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 248, in _update def _update(self, value): self._text_box.SetValue(value); self._update_color() File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_controls.py", line 1754, in SetValue return _controls_.TextCtrl_SetValue(*args, **kwargs) TypeError: String or Unicode type required Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 102, in widget.Bind(EVT_DATA, lambda x: self._update(x.data)) File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 181, in _update def _update(self, value): self._slider.SetValue(int(round(value))) TypeError: a float is required On Thu, Jun 12, 2014 at 6:39 PM, Alan Gauld wrote: > On 12/06/14 05:51, jason sam wrote: > >> Hi All, >> I am new to wxPython.I have made a simple GUI that contains a button and >> by pressing that button i am calling another .py >> file(top_block.py)... >> > > No you are not. > You are importing it and then trying to access something called start > which doesn't exist. importing is not the same as running a script. > For a start the _name__ attribute will not be __main__ so your if clause > at the bottom won't be executed. But the class definition > code will have been run so you can access and create those classes > from your wxGUI module. > > However, it is very strange practice to try to create a second > GUI program from a first (not totally unheard of but uncommon). > More usually you would define a dialog and open that from > your Execute method. > > > top_block.start() >> >> The error is: >> >> AttributeError: 'module' object has no attribute 'start' >> > > Please always post the full error message not just a summary. > It has all sorts of useful extra details in there. In this case it doesn't > matter because as the error says your module has no start attribute - > because you didn't create one. > > > > code are also welcome.The same error is received when i >> use'top_block.run()'.Is there any alternative to this commands? >> > > Yep, you didn't define a run() function either. > modules can only execute what you define within them (and one or two > special case attributes defined by Python; all starting with __ > > Thee are several other oddities in your code but this is > probably enough for now. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > #!/usr/bin/env python ## # Gnuradio Python Flow Graph # Title: UHD FFT # Author: Example # Description: UHD FFT Waveform Plotter # Generated: Sun May 25 01:21:34 2014 ## from gnuradio import analog from gnuradio import blocks from gnuradio import eng_notation from gnuradio import gr from gnuradio import wxgui from gnuradio.eng_option import eng_option from gnuradio.fft import window from gnuradio.filter import firdes from gnuradio.wxgui import fftsink2 from gnuradio.wxgui import forms from gnuradio.wxgui import scop
Re: [Tutor] AttributeError: 'module' object has no attribute 'start'
The last question i hadn't asked yet..and if u are not interested in answering the question then also refrain from commenting please! On Thu, Jun 12, 2014 at 9:47 PM, Mark Lawrence wrote: > On 12/06/2014 05:51, jason sam wrote: > >> Hi All, >> I am new to wxPython.I have made a simple GUI that contains a button and >> by pressing that button i am calling another .py >> file(top_block.py)...But i am getting error using the command: >> >> top_block.start() >> >> The error is: >> >> AttributeError: 'module' object has no attribute 'start' >> >> The code is as attached.I am using python 2.7.3.Any improvements in the >> code are also welcome.The same error is received when i >> use'top_block.run()'.Is there any alternative to this commands? >> >> Regards, >> Ali >> >> > I suspect that you'll get better answers on the wxpython list, especially > consdiering that you asked there all of two minutes before asking here. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus > protection is active. > http://www.avast.com > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
On 13/06/14 08:21, diliup gabadamudalige wrote: In my code there are many dictionaries and lists which are used in various functions. Is it better/pythonic/efficient to have these inside the function itself or declared at the beginning of the program in which case they will be global? If you are sharing a set of data structures between several functions thats often a sign they should be in a class. The data then becomes the class instance attributes and the functions become the methods. global constants and variable have memory allocated to them but when declared inside a function are created on the fly, used and discarded. That's not usually the main issue when deciding for global/local. Its more about the maintainability of the code and the over use of side effects which makes the code difficult to read. If the data is read only then things are not so bad and globals can be an OK solution. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to test mobile apps using python
Hi, I would like to know how we could test android mobile apps using python. Lets say I want to test my skype app. How can we go about? Regards, Reuben ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to test mobile apps using python
On 13/06/14 09:27, Reuben wrote: I would like to know how we could test android mobile apps using python. Lets say I want to test my skype app. How can we go about? Presumably you are writing these mobile apps in Python? If so what are you using to build the mobile app? What library/framework etc? Its not a feature of standard Python I'm aware of, so it would be useful to have the context. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to test mobile apps using python
These apps are native android apps downloaded from google play. Skype being an example. As far as I am aware all these apps are written in Java. But my problem statement requires that we test this app using python script. Regards, Reuben On Fri, Jun 13, 2014 at 1:32 AM, Alan Gauld wrote: > On 13/06/14 09:27, Reuben wrote: > > I would like to know how we could test android mobile apps using python. >> Lets say I want to test my skype app. How can we go about? >> > > Presumably you are writing these mobile apps in Python? > If so what are you using to build the mobile app? What library/framework > etc? > > Its not a feature of standard Python I'm aware of, so it > would be useful to have the context. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to test mobile apps using python
On 13/06/14 09:38, Reuben wrote: These apps are native android apps downloaded from google play. Skype being an example. As far as I am aware all these apps are written in Java. But my problem statement requires that we test this app using python script. What kind of "testing" do you have in mind? Code testing will have been done by the developer in their development tools. I assume you only want to test performance or somesuch? There is a Python interpreter/IDE available for Android (QPython) but how you would interact with a native app I don't know. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
CCing the tutor list. Please use ReplyAll when responding to the list. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos > > From: diliup gabadamudalige >To: Alan Gauld >Sent: Friday, 13 June 2014, 9:32 >Subject: Re: [Tutor] global variables/constants versus volatile >variables/constants > > > >thanks Alan! > >in the program that i am writing i used a class called variables and declare >all variables i use in the program in the def __init__ section. >so all variables which otherwise would have to be declared as global so that >they can be modified by other functions are in a class. >Is this ok? >The strings, lists and dictionaries which are not read only are used as locals >in functions and classes. >If there is a string, list or dict. which is used in several functions is it >better to use a global or several locals? > > > >On Fri, Jun 13, 2014 at 1:25 PM, Alan Gauld wrote: > >On 13/06/14 08:21, diliup gabadamudalige wrote: >> >> >>In my code there are many dictionaries and lists which are used in >>>various functions. Is it better/pythonic/efficient to have these inside >>>the function itself or declared at the beginning of the program in which >>>case they will be global? >>> >> If you are sharing a set of data structures between several functions thats often a sign they should be in a class. The data then becomes the class instance attributes and the functions become the methods. >> >> >> >>global constants and variable have memory allocated to them but when >>>declared inside a function are created on the fly, used and discarded. >>> >> That's not usually the main issue when deciding for global/local. >>Its more about the maintainability of the code and the over >>use of side effects which makes the code difficult to read. >>If the data is read only then things are not so bad and globals >>can be an OK solution. >> >>hth >>-- >>Alan G >>Author of the Learn to Program web site >>http://www.alan-g.me.uk/ >>http://www.flickr.com/photos/alangauldphotos >> >>___ >>Tutor maillist - Tutor@python.org >>To unsubscribe or change subscription options: >>https://mail.python.org/mailman/listinfo/tutor >> > > > >-- > >Diliup Gabadamudalige > >http://www.diliupg.com >http://soft.diliupg.com/ > >** >This e-mail is confidential. It may also be legally privileged. If you are not >the intended recipient or have received it in error, please delete it and all >copies from your system and notify the sender immediately by return e-mail. >Any unauthorized reading, reproducing, printing or further dissemination of >this e-mail or its contents is strictly prohibited and may be unlawful. >Internet communications cannot be guaranteed to be timely, secure, error or >virus-free. The sender does not accept liability for any errors or omissions. >** > > > >___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem reading large files in binary mode
Mirage Web Studio wrote: > Try reading the file in chunks instead: > > CHUNKSIZE = 2**20 > hash = hashlib.md5() > while True: > chunk = f.read(CHUNKSIZE) > if not chunk: > break > hash.update(chunk) > hashvalue = hash.hexdigest() > > > Thank you peter for the above valubale reply. but shouldn't read() by > itself work because i have enough memory to load it or should it be a bug. I think you are right. At least you should get a MemoryError (the well- behaved way of the Python interpreter to say that it cannot allocate enough memory) while your description hints at a segmentation fault. A quick test with the Python versions I have lying around: $ python -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError $ python3.3 -c 'open("bigfile", "rb").read()' Segmentation fault $ python3.3 -V Python 3.3.2+ $ python3.4 -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError So the bug occurs in 3.3 at least up to 3.3.2. If you don't have the latest bugfix release Python 3.3.4 you can try and install that or if you are not tied to 3.3 update to 3.4.1. Note that you may still run out of memory, particularly if you are using the 32 bit version. Also it is never a good idea to load a lot of data into memory when you intend to use it just once. Therefore I recommend that you calculate the checksum the way that I have shown in the example. PS: There was an email in my inbox where eryksun suggests potential improvements to my code: > You might see better performance if you preallocate a bytearray and > `readinto` it. On Windows, you might see even better performance if > you map sections of the file using mmap; the map `length` needs to be > a multiple of ALLOCATIONGRANULARITY (except the residual) to set the > `offset` for a sliding window. While I don't expect significant improvements since the problem is "I/O- bound", i. e. the speed limit is imposed by communication with the harddisk rather than the Python interpreter, you may still find it instructive to compare the various approaches. Another candidate when you are working in an environment where the md5sum utility is available is to delegate the work to the "specialist": hashvalue = subprocess.Popen( ["md5sum", filename], stdout=subprocess.PIPE).communicate()[0].split()[0].decode() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to test mobile apps using python
On Fri, Jun 13, 2014 at 01:27:07AM -0700, Reuben wrote: > Hi, > > I would like to know how we could test android mobile apps using python. > Lets say I want to test my skype app. How can we go about? This forum is for learning the Python language, and unfortunately there's no "test_android_app" function in the language. So you're asking a very specialized question: it requires not just knowledge of Python, or of testing frameworks in Python, but of testing frameworks in Python on the Android platform. If you were writing your apps in Python, I'd suggest just using the doctest and unittest modules. For testing native Android apps, I don't know. If you can drive the app using Python, then you can write tests using unittest. https://docs.python.org/2/library/unittest.html unittest is based very closely on Java unit testing, so if you know Java, you should find it quite familiar. Other than that, you may find some help from a Jython discussion group, they might be able to suggest how to test Java apps from Python. Or from a Kivvy group, they may be able to suggest how to test apps on Android. Good luck. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
diliup gabadamudalige Wrote in message: > there are many dictionaries and lists which are used in various functions. Is it better/pythonic/efficient to have these inside the function itself or declared at the beginning of the program in which case they will be global? They are all read only. I understand that global constants and variable have memory allocated to them but when declared inside a function are created on the fly, used and discarded. > (Please use text mail to post here, not html. Html messages can cause several kinds of problems in a text mailing list. ) If they are read-only, you don't want to waste time recreating them over and over. Neither do you want to be repeating the code that initializes them. So make them either global or a class member, and be sure to name them all caps to signify they shouldn't be altered. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote: > Hi All! > Hope everyone is well. > > In my code there are many dictionaries and lists which are used in various > functions. Is it better/pythonic/efficient to have these inside the > function itself or declared at the beginning of the program in which case > they will be global? They are all read only. I understand that global > constants and variable have memory allocated to them but when declared > inside a function are created on the fly, used and discarded. Please > enlighten me further on this and correct me if i'm wrong. A good question. Depending on the size of these dictionaries and lists, worrying about efficiency here may be premature optimization. As they say: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." -- Donald Knuth "The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." -- Michael A. Jackson "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity." -- W.A. Wulf If these lists and dicts are small, say, fewer than a dozen items, the time to create and destroy them is probably trivial, especially if you construct them from constant literals. In that case, it's a matter of personal taste whether you prefer them as global constants or local to a function. But if it takes a long time to build the list, then you definitely should move it outside the function and perform the initialisation step only once: # This may take a while... PRIMES = [prime(i) for i in range(1, 101)] If your lists really are read-only, then you should consider turning them into tuples: # Not this: SOME_ITEMS = ["this", "that", "another"] # Better: SOME_ITEMS = ("this", "that", "another") One advantage of the tuple is that in recent versions of Python, the tuple may be compiled as a constant instead of being built at runtime. This is from Python 2.7: py> from dis import dis py> code = compile("x = [2, 4, 8]", "", "exec") py> dis(code) 1 0 LOAD_CONST 0 (2) 3 LOAD_CONST 1 (4) 6 LOAD_CONST 2 (8) 9 BUILD_LIST 3 12 STORE_NAME 0 (x) 15 LOAD_CONST 3 (None) 18 RETURN_VALUE py> code = compile("x = (2, 4, 8)", "", "exec") py> dis(code) 1 0 LOAD_CONST 4 ((2, 4, 8)) 3 STORE_NAME 0 (x) 6 LOAD_CONST 3 (None) 9 RETURN_VALUE -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Friday, June 13, 2014 1:45 PM > Subject: Re: [Tutor] global variables/constants versus volatile > variables/constants > > On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote: >> Hi All! >> Hope everyone is well. >> >> In my code there are many dictionaries and lists which are used in various >> functions. Is it better/pythonic/efficient to have these inside the >> function itself or declared at the beginning of the program in which case >> they will be global? They are all read only. I understand that global >> constants and variable have memory allocated to them but when declared >> inside a function are created on the fly, used and discarded. Please >> enlighten me further on this and correct me if i'm wrong. > > A good question. > > Depending on the size of these dictionaries and lists, worrying about > efficiency here may be premature optimization. As they say: > > > "We should forget about small efficiencies, say about 97% of > the time: premature optimization is the root of all evil." > -- Donald Knuth The other day I used collections.namedtuple and I re-initialized Record (see below) with every function*) call. Bad idea! It looks nicer because I did not need a global (and globals are baaad, mkay?), but it was *much* slower. I processed a log of a few million lines, I think. # bad --> time-consuming import collections def do_something_with(raw_record): Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) return Record(*raw_record.split()) # better --> even though it uses a global variable import collections Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) def do_something_with(raw_record): return Record(*raw_record.split()) *) the function contained more code, of course! Albert-Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'module' object has no attribute 'start'
jason sam Wrote in message: > (Please use text mail, as html gets distorted and/or jumbled. Also, many people here cannot see attachments, so you should paste them into your text message. And be sure to put a marker before each file indicating it's original filename) self.update_grid() File "/usr/local/lib/python2.7/dist-packages/g nuradio/wxgui/fft_window.py", line 401, in update_grid baseband_freq - sample_rate/2.0, TypeError: unsupported operand type(s) for -: 'MyFrame' and 'float' You have several tracebacks, but perhaps fixing this one will give you a head start on the others. > Your variable baseband_freq is not a float, but an instance of class > MyFrame. Perhaps you had intended to call a method of that class, instead > of using the class instance. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > The other day I used collections.namedtuple and I re-initialized > Record (see below) with every function*) call. Bad idea! It looks > nicer because I did not need a global (and globals are baaad, mkay?), > but it was *much* slower. I processed a log of a few million lines, I > think. > > # bad --> time-consuming > import collections > > def do_something_with(raw_record): > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) > return Record(*raw_record.split()) Look at how much work you do here. First, you create a long string of the form: "v000 v001 v002 v003 ... v099" representing 1000 v-digits names. Then you create a brand new Record class that takes those 100 v-digits names as arguments. Creating that class requires building a string, parsing it as Python code, and then running it. (You're not expected to know that, but if you read the source code for namedtuple you will see that's how it works.) So creating that class is slow. Every time you call the function, it builds a new "v000 ... v099" string, from scratch, then builds a new class, also from scratch, and finally populates an instance of that class with 100 values from the raw_record. Only that last step needs to be done inside the function. > # better --> even though it uses a global variable > import collections > > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) [Aside: you may find it easier to debug problems with this if you give the namedtuple class a sensible name, like "Record", rather than "_".] How is that a global *variable*? It's a global name, "Record", but it is no more a "variable" than it would be if you did: class Record(tuple): def __new__(cls, v000, v001, v002, ... , v099): # code goes here @property def v000(self): return self[0] # likewise for v001, v002, ... v099 # plus additional methods namedtuple is a factory function which creates a class. Buried deep within it is a class statement, just as if you had written the class yourself. Normally, when you create a class, you don't treat it as a variable, you treat it as a constant, like functions. That is no different from classes you create with the class keyword. So "global variables are bad" doesn't apply because it's not a variable. Even if it were a variable, what really matters is not that it gets stored in the global scope, but whether or not it gets explicitly passed to functions as arguments, or implicitly modified secretly behind the scenes. For example: # Not bad data = ["put", "stuff", "here"] process(data) do_things_with(data) # Bad, for various reasons data = ["put", "stuff", "here"] process() # process what? do_things_with() # What are we doing things with? In the first case, "data" may be stored in the global scope, but inside each function it is treated as a regular local variable. Let's contrast how one might operate on a second set of data in each case: # Too easy process(some_other_data) # Ouch, this is painful save_data = data data = some_other_data process() data = save_data # restore the previous value Global variables aren't bad because Moses came down from the mountains with a stone tablet that declares that they are bad. They're bad because they cause excessive coupling, they operate by side-effect, they spoil idepotent code, and they are implicit instead of explicit. > def do_something_with(raw_record): > return Record(*raw_record.split()) Much more sensible! -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'module' object has no attribute 'start'
On 12/06/2014 17:51, jason sam wrote: The last question i hadn't asked yet..and if u are not interested in answering the question then also refrain from commenting please! On Thu, Jun 12, 2014 at 9:47 PM, Mark Lawrence mailto:breamore...@yahoo.co.uk>> wrote: On 12/06/2014 05:51, jason sam wrote: Hi All, I am new to wxPython.I have made a simple GUI that contains a button and by pressing that button i am calling another .py file(top_block.py)...But i am getting error using the command: top_block.start() The error is: AttributeError: 'module' object has no attribute 'start' The code is as attached.I am using python 2.7.3.Any improvements in the code are also welcome.The same error is received when i use'top_block.run()'.Is there any alternative to this commands? Regards, Ali I suspect that you'll get better answers on the wxpython list, especially consdiering that you asked there all of two minutes before asking here. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- Please don't top post on this list. Please don't try telling me what to do or not do. Thanks in anticipation. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'module' object has no attribute 'start'
> Please don't top post on this list. Please don't try telling me what to > do or not do. Thanks in anticipation. Oh the irony. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Friday, June 13, 2014 3:08 PM > Subject: Re: [Tutor] global variables/constants versusvolatile > variables/constants > > On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > >> The other day I used collections.namedtuple and I re-initialized >> Record (see below) with every function*) call. Bad idea! It looks >> nicer because I did not need a global (and globals are baaad, mkay?), >> but it was *much* slower. I processed a log of a few million lines, I >> think. >> >> # bad --> time-consuming >> import collections >> >> def do_something_with(raw_record): >> Record = collections.namedtuple("_", " > ".join("v%%03d" % i for i in range(100))) >> return Record(*raw_record.split()) > > Look at how much work you do here. First, you create a long string of > the form: > > "v000 v001 v002 v003 ... v099" > > representing 1000 v-digits names. Then you create a brand new Record > class that takes those 100 v-digits names as arguments. Creating that > class requires building a string, parsing it as Python code, and then > running it. (You're not expected to know that, but if you read the > source code for namedtuple you will see that's how it works.) So > creating that class is slow. Every time you call the function, it builds > a new "v000 ... v099" string, from scratch, then builds a new class, > also from scratch, and finally populates an instance of that class with > 100 values from the raw_record. > > Only that last step needs to be done inside the function. Hmm, if I create the namedtuple with 'verbose=True' it *really* makes clear why it took so much longer. (what's '_property' btw? I know 'property' and the newer decorator wih the same name, but not _property). In [1]: import collections In [2]: Record = collections.namedtuple("_", " ".join("v%03d" % i for i in range(100)),verbose=True) class _(tuple): '_(v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)' __slots__ = () _fields = ('v000', 'v001', 'v002', 'v003', 'v004', 'v005', 'v006', 'v007', 'v008', 'v009', 'v010', 'v011', 'v012', 'v013', 'v014', 'v015', 'v016', 'v017', 'v018', 'v019', 'v020', 'v021', 'v022', 'v023', 'v024', 'v025', 'v026', 'v027', 'v028', 'v029', 'v030', 'v031', 'v032', 'v033', 'v034', 'v035', 'v036', 'v037', 'v038', 'v039', 'v040', 'v041', 'v042', 'v043', 'v044', 'v045', 'v046', 'v047', 'v048', 'v049', 'v050', 'v051', 'v052', 'v053', 'v054', 'v055', 'v056', 'v057', 'v058', 'v059', 'v060', 'v061', 'v062', 'v063', 'v064', 'v065', 'v066', 'v067', 'v068', 'v069', 'v070', 'v071', 'v072', 'v073', 'v074', 'v075', 'v076', 'v077', 'v078', 'v079', 'v080', 'v081', 'v082', 'v083', 'v084', 'v085', 'v086', 'v087', 'v088', 'v089', 'v090', 'v091', 'v092', 'v093', 'v094', 'v095', 'v096', 'v097', 'v098', 'v099') def __new__(_cls, v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099): 'Create new instance of _(v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)' return _tuple.__new__(_cls, (v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v03
Re: [Tutor] Problem reading large files in binary mode
Thank you, i will keep all that in mind. My python version is 3.3.5 George On 13-06-2014 16:07, Peter Otten wrote: Mirage Web Studio wrote: Try reading the file in chunks instead: CHUNKSIZE = 2**20 hash = hashlib.md5() while True: chunk = f.read(CHUNKSIZE) if not chunk: break hash.update(chunk) hashvalue = hash.hexdigest() Thank you peter for the above valubale reply. but shouldn't read() by itself work because i have enough memory to load it or should it be a bug. I think you are right. At least you should get a MemoryError (the well- behaved way of the Python interpreter to say that it cannot allocate enough memory) while your description hints at a segmentation fault. A quick test with the Python versions I have lying around: $ python -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError $ python3.3 -c 'open("bigfile", "rb").read()' Segmentation fault $ python3.3 -V Python 3.3.2+ $ python3.4 -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError So the bug occurs in 3.3 at least up to 3.3.2. If you don't have the latest bugfix release Python 3.3.4 you can try and install that or if you are not tied to 3.3 update to 3.4.1. Note that you may still run out of memory, particularly if you are using the 32 bit version. Also it is never a good idea to load a lot of data into memory when you intend to use it just once. Therefore I recommend that you calculate the checksum the way that I have shown in the example. PS: There was an email in my inbox where eryksun suggests potential improvements to my code: You might see better performance if you preallocate a bytearray and `readinto` it. On Windows, you might see even better performance if you map sections of the file using mmap; the map `length` needs to be a multiple of ALLOCATIONGRANULARITY (except the residual) to set the `offset` for a sliding window. While I don't expect significant improvements since the problem is "I/O- bound", i. e. the speed limit is imposed by communication with the harddisk rather than the Python interpreter, you may still find it instructive to compare the various approaches. Another candidate when you are working in an environment where the md5sum utility is available is to delegate the work to the "specialist": hashvalue = subprocess.Popen( ["md5sum", filename], stdout=subprocess.PIPE).communicate()[0].split()[0].decode() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
Thank you all for these elaborate explanations. so would it be wrong to assume that, 1. the list or dict if 'large' and not mutated is better declared once in a Class and used throughout the program? or 2. Lists that are read only are better off being tuples? or 3.the list or dict if 'large' and not mutated is better declared once as a global variable and used throughout the program? The variables, lists carrying variables, and dict. carrying variables are declared as a class. So why not include all above in the same class? Variables and constants which are used throughout the program in one class declared before the program enters the main executing loop? wouldn't this be fast and the memory usage static up to a point (for all declared items in the variables class)? On Fri, Jun 13, 2014 at 9:42 PM, diliup gabadamudalige wrote: > Thank you all for these elaborate explanations. > so would it be wrong to assume that, > 1. the list or dict if 'large' and not mutated is better declared once in > a Class and used throughout the program? > or > 2. Lists that are read only are better off being tuples? > or > 3.the list or dict if 'large' and not mutated is better declared once as a > global variable and used throughout the program? > > The variables, lists carrying variables, and dict. carrying variables are > declared as a class. So why not include all above in the same class? > Varables and constans which are used throughout the program in one class > declared beofr the program enters the main executing loop? > wouldn't this be fast and the memory usage static up to a point (for all > declared items in the variables class)? > > > On Fri, Jun 13, 2014 at 6:38 PM, Steven D'Aprano > wrote: > >> On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: >> >> > The other day I used collections.namedtuple and I re-initialized >> > Record (see below) with every function*) call. Bad idea! It looks >> > nicer because I did not need a global (and globals are baaad, mkay?), >> > but it was *much* slower. I processed a log of a few million lines, I >> > think. >> > >> > # bad --> time-consuming >> > import collections >> > >> > def do_something_with(raw_record): >> >Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in >> range(100))) >> >return Record(*raw_record.split()) >> >> Look at how much work you do here. First, you create a long string of >> the form: >> >> "v000 v001 v002 v003 ... v099" >> >> representing 1000 v-digits names. Then you create a brand new Record >> class that takes those 100 v-digits names as arguments. Creating that >> class requires building a string, parsing it as Python code, and then >> running it. (You're not expected to know that, but if you read the >> source code for namedtuple you will see that's how it works.) So >> creating that class is slow. Every time you call the function, it builds >> a new "v000 ... v099" string, from scratch, then builds a new class, >> also from scratch, and finally populates an instance of that class with >> 100 values from the raw_record. >> >> Only that last step needs to be done inside the function. >> >> >> > # better --> even though it uses a global variable >> > import collections >> > >> > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in >> range(100))) >> >> [Aside: you may find it easier to debug problems with this if you give >> the namedtuple class a sensible name, like "Record", rather than "_".] >> >> How is that a global *variable*? It's a global name, "Record", but it is >> no more a "variable" than it would be if you did: >> >> class Record(tuple): >> def __new__(cls, v000, v001, v002, ... , v099): >> # code goes here >> >> @property >> def v000(self): >> return self[0] >> >> # likewise for v001, v002, ... v099 >> # plus additional methods >> >> >> namedtuple is a factory function which creates a class. Buried deep >> within it is a class statement, just as if you had written the class >> yourself. Normally, when you create a class, you don't treat it as a >> variable, you treat it as a constant, like functions. That is no >> different from classes you create with the class keyword. So "global >> variables are bad" doesn't apply because it's not a variable. >> >> Even if it were a variable, what really matters is not that it gets >> stored in the global scope, but whether or not it gets explicitly passed >> to functions as arguments, or implicitly modified secretly behind the >> scenes. For example: >> >> # Not bad >> data = ["put", "stuff", "here"] >> process(data) >> do_things_with(data) >> >> >> # Bad, for various reasons >> data = ["put", "stuff", "here"] >> process() # process what? >> do_things_with() # What are we doing things with? >> >> >> >> In the first case, "data" may be stored in the global scope, but inside >> each function it is treated as a regular local variable. Let's contrast >> how one might operate on a second set of data in ea
Re: [Tutor] global variables/constants versus volatile variables/constants
Thank you all for these elaborate explanations. so would it be wrong to assume that, 1. the list or dict if 'large' and not mutated is better declared once in a Class and used throughout the program? or 2. Lists that are read only are better off being tuples? or 3.the list or dict if 'large' and not mutated is better declared once as a global variable and used throughout the program? The variables, lists carrying variables, and dict. carrying variables are declared as a class. So why not include all above in the same class? Varables and constans which are used throughout the program in one class declared beofr the program enters the main executing loop? wouldn't this be fast and the memory usage static up to a point (for all declared items in the variables class)? On Fri, Jun 13, 2014 at 6:38 PM, Steven D'Aprano wrote: > On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > > > The other day I used collections.namedtuple and I re-initialized > > Record (see below) with every function*) call. Bad idea! It looks > > nicer because I did not need a global (and globals are baaad, mkay?), > > but it was *much* slower. I processed a log of a few million lines, I > > think. > > > > # bad --> time-consuming > > import collections > > > > def do_something_with(raw_record): > >Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) > >return Record(*raw_record.split()) > > Look at how much work you do here. First, you create a long string of > the form: > > "v000 v001 v002 v003 ... v099" > > representing 1000 v-digits names. Then you create a brand new Record > class that takes those 100 v-digits names as arguments. Creating that > class requires building a string, parsing it as Python code, and then > running it. (You're not expected to know that, but if you read the > source code for namedtuple you will see that's how it works.) So > creating that class is slow. Every time you call the function, it builds > a new "v000 ... v099" string, from scratch, then builds a new class, > also from scratch, and finally populates an instance of that class with > 100 values from the raw_record. > > Only that last step needs to be done inside the function. > > > > # better --> even though it uses a global variable > > import collections > > > > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) > > [Aside: you may find it easier to debug problems with this if you give > the namedtuple class a sensible name, like "Record", rather than "_".] > > How is that a global *variable*? It's a global name, "Record", but it is > no more a "variable" than it would be if you did: > > class Record(tuple): > def __new__(cls, v000, v001, v002, ... , v099): > # code goes here > > @property > def v000(self): > return self[0] > > # likewise for v001, v002, ... v099 > # plus additional methods > > > namedtuple is a factory function which creates a class. Buried deep > within it is a class statement, just as if you had written the class > yourself. Normally, when you create a class, you don't treat it as a > variable, you treat it as a constant, like functions. That is no > different from classes you create with the class keyword. So "global > variables are bad" doesn't apply because it's not a variable. > > Even if it were a variable, what really matters is not that it gets > stored in the global scope, but whether or not it gets explicitly passed > to functions as arguments, or implicitly modified secretly behind the > scenes. For example: > > # Not bad > data = ["put", "stuff", "here"] > process(data) > do_things_with(data) > > > # Bad, for various reasons > data = ["put", "stuff", "here"] > process() # process what? > do_things_with() # What are we doing things with? > > > > In the first case, "data" may be stored in the global scope, but inside > each function it is treated as a regular local variable. Let's contrast > how one might operate on a second set of data in each case: > > # Too easy > process(some_other_data) > > > # Ouch, this is painful > save_data = data > data = some_other_data > process() > data = save_data # restore the previous value > > > Global variables aren't bad because Moses came down from the mountains > with a stone tablet that declares that they are bad. They're bad because > they cause excessive coupling, they operate by side-effect, they spoil > idepotent code, and they are implicit instead of explicit. > > > > > > def do_something_with(raw_record): > >return Record(*raw_record.split()) > > Much more sensible! > > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ** Th
Re: [Tutor] global variables/constants versus volatile variables/constants
On 13/06/14 19:20, diliup gabadamudalige wrote: I declare the following dict at the beginning of the program. KEY_SIGNATURES = {"C": [], "G": ["F"], "D": ["F", "C"], "A": ["F", "C", "G"], "E": ["F", "C", "G", "D"], "B": ["F", "C", "G", "D", "A"], "F#": now in the functions when ever I need to access i just use it. eg: sharps_or_flats = KEY_SIGNATURES[key] or I can create it like this class Variables(): def __init__(self, adv): self.KEY_SIGNATURES = {"C": [], "G": ["F"], now I call the class with v=Variables() now the same dict is v.Key_SIGNATURES my questions: which is better to use and why? As always there is no absolute answer. It depends on what you are doing in a wider context. However from your message you may be missing the point of the class solution. The pointy of a class is to bring together the variables *and the functions* that operate on those variables. So you should not be doing v.Key_SIGNATURES from functions outside the class, rather you should be defining your functions as methods of the class and inside those methods using self.Key_SIGNATURES Also a class called 'variables' is usually a bad design smell. The class should be called something meaningful in the context of your program. Something that represents the data and operations that you have captured within it. If you discover you have captured two unrelated concepts then split the class into two. That will encourage reuse but also make your code much more readable. Do both remain in memory(separately of course) till I exit the program once I run it? Will memory usage be the same? For data this small what happens in memory should be the least of your worries. Its much more important to get a design that is readable, maintainable and reliable. Only when you have proved there is a memory issue to solve do you need to start worrying about it. will the creation of the dict take the same time in both above? But if you are really worried about it use the timeit module or the profiler to find out, don't guess or rely on others guesses. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
On Fri, Jun 13, 2014 at 09:42:34PM +0530, diliup gabadamudalige wrote: > Thank you all for these elaborate explanations. > so would it be wrong to assume that, > 1. the list or dict if 'large' and not mutated is better declared once in a > Class and used throughout the program? There is *no* advantage to writing this: class Foo: data = ['a', 'large', 'list'] result = function(Foo.data) instead of this: data = ['a', 'large', 'list'] result = function(data) Just dumping things into a class is not very helpful. You should ask yourself: * Does my code represent a "thing", like a string, or a web server, or a customer record? Then a class might be a good solution. * Does this thing have data and functions that go together? For example, a customer record might have data like Account Balance, and a function like update_account_balance. Then a class might be a good solution. * Could I have more than one of these "things" at the same time? For example, you obviously could have more than one string or customer record. Then a class is probably a good solution. If none of these things are true, then a class is probably a bad solution. > or > 2. Lists that are read only are better off being tuples? Possibly. It depends. As a general rule, tuples may be used for heterogeneous data, lists should be used for homogeneous data. What do I mean by this? This is a good use for a tuple: customer = (name, address, phone_number, status, balance) You have a fixed number of fields, and each field means something different (they are hetrogeneous). You should not use a list for something like this. This is a good use for a list: [value, another_value, a_third_value, ...] There is an arbitrary number of items, and each item represents the same kind of thing (they are homogeneous). Don't think of types, like "all the items are floats", but instead think "all the items are a person's height". As a micro-optimization, you might treat tuple as a frozen-list. That's not so unusual that you will confuse people by it, but it is a little unusual, and a matter of personal taste. > or > 3.the list or dict if 'large' and not mutated is better declared once as a > global variable and used throughout the program? If it's not mutated, it isn't really a *variable* is it? But yes, I would consider this the simplest thing that can work. This would be my preference. > The variables, lists carrying variables, and dict. carrying variables are > declared as a class. So why not include all above in the same class? > Varables and constans which are used throughout the program in one class > declared beofr the program enters the main executing loop? > wouldn't this be fast and the memory usage static up to a point (for all > declared items in the variables class)? I'm afraid I don't understand this paragraph. Can you try explaining more carefully please? (By the way: please trim unnecessary quoted text. Your email contained dozens of lines of double-quoted > > comments that had nothing to do with the questions you asked. If the comments aren't relevant, please delete them. Thank you.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
Albert-Jan, I've been meaning to ask for a long time... I don't suppose you're hitting "Forward" rather than "Reply" in your posts are you? Because I've never seen replies from anyone else use the same style as your replies. Further comments below. On Fri, Jun 13, 2014 at 10:14:46AM -0700, Albert-Jan Roskam wrote: > Hmm, if I create the namedtuple with 'verbose=True' it *really* makes clear > why it took so much longer. > (what's '_property' btw? I know 'property' and the newer decorator wih the > same name, but not _property). _property is just property renamed. If you read the source code to namedtuple, you will see something like the line: _property = property or possibly: from builtins import property as _property That's likely done to make it a local variable, and so a little faster (although I don't know why the author bothered, it will make so little difference), or possibly to prevent clashes in case the caller defines a field name called "property". I'm not sure, but they are my guesses. [...] > v000 = _property(_itemgetter(0), doc='Alias for field number 0') > > v001 = _property(_itemgetter(1), doc='Alias for field number 1') > > v002 = _property(_itemgetter(2), doc='Alias for field number 2') [snip 97 more lines] Yes, thank you, we get the picture. No need to show 100 examples. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] global variables/constants versus volatile variables/constants
On 14Jun2014 11:21, Steven D'Aprano wrote: On Fri, Jun 13, 2014 at 09:42:34PM +0530, diliup gabadamudalige wrote: 2. Lists that are read only are better off being tuples? Possibly. It depends. As a general rule, tuples may be used for heterogeneous data, lists should be used for homogeneous data. To make this less clear, I would say this is a useful rule some of the time. It certainly isn't the only criterion one might use for choosing between a tuple and a list. What do I mean by this? This is a good use for a tuple: customer = (name, address, phone_number, status, balance) You have a fixed number of fields, and each field means something different (they are hetrogeneous). You should not use a list for something like this. Were I using the "heterogeneous data" rule I would be advocaing going to whole hog and using a "namedtuple" from the "collections" module. This has the advantage of giving each element of the tuple a nice name so that you can refer to "customer.address" instead of using magic indices like "customer[1]". That makes the code easier to read and easier to write. But if you're not using namedtuple-like attributes to access the tuple elements, then I would not be considering the "heterogeneous data" rule to be as relevant in my choice of tuple or list. There are some advantages to using a tuple as a frozen list, which can't have its values modified nor have its size changed. Specificly, if that is your intent and your code later tried to change the values (because it is buggy) then Python will raise an exception immediately, making it easier to find your mistake. If you stick to a list for this situation then the buggy changes will go ahead and the error will only be apparent much later when you get the wrong results. Fail early, fail often! Conversely, there are downsides to using tuples as frozen lists. The most obvious is that you can't add lists and tuples together, just lists and lists. If you need to add tuples and lists all the time then the surround fiddliness required may be enough to push you back to using lists throughout. Finally, another argument against mixing tuples and lists is simply that it may be early optimisation. Unless you're benefiting from the "you can't accidentally modify a tuple later" effect, just write it all with lists; it will be simpler. You can always come back later after the code is functioning correctly and think about tuples. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor