Re: [Tutor] global variables/constants versus volatile variables/constants
Thank you all for those great clarifications. I learnt a lot from these. My roots in programming are from the early 80s and with a gap of nearly 30 years till I restarted again last year in March with Python. :) So some of the new concepts like classes are a bit alien to me but I am catching on. I made a mistake by using the word variables on variables and constants both. My design structure would most probably be quite unconventional to most like Allen Gauld says. I am still learning and I like to jump into the deep end and learn to swim there and so far have managed to stay alive! :) I have gained immensely from these few emails and hope I can gain more knowledge to write better code. Say if I have a lot of Lists, strings and variables used to carry data to and from various functions why can't I have a class with all these in it? I thought on the lines of blood carrying various things to different organs in the body. One common container class. Containers for data exchange between all classes and functions. so I declare class Containers(): def __init__(self): self.last_selected_scale= "" self.scale_notes = "" self.arpeggionotes = "" self.handplayed_notes = [] self.MIDIscale = [] # the MIDI scale to be played is here self.play_MIDIscale = False self.play_MIDIarpeggio = False self.play_MIDI_scale_note = False self.play_MIDI_arpeggio_note = False self.MIDInote_inscale = 0 now i initiate the class with v=Containers() now say if I play the MIDI keyboard, I collect the notes played which will be appended to the list v.hanplayednotes. This will be done from a function which will scan the keyboard and append played notes to that list. This is how I have used these variables, lists and strings in this program. So far it has run error free. Is this not correct or bad design. I know it is not the normal way of using class but is it wrong? Thank you all in advance. -- 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] global variables/constants versus volatile variables/constants
On Sat, Jun 14, 2014 at 06:23:08PM +0530, diliup gabadamudalige wrote: > Say if I have a lot of Lists, strings and variables used to carry data to > and from various functions why can't I have a class with all these in it? Of course you *can*, but you *should not*. Read on... [...] > so I declare > > class Containers(): > def __init__(self): > self.last_selected_scale= "" > self.scale_notes = "" > self.arpeggionotes = "" etc. > now i initiate the class with > v=Containers() So now you write code where everything you use starts with "v.". What does the v. do? How does it help your programming? Answer: it doesn't. It just makes an extra two characters to type. Compare the code you would write if you just defined the variables directly: # Option 1 last_selected_scale = "" scale_notes = "" arpeggionotes = "" do_something(last_selected_scale) do_another_thing(scale_notes, arpeggionotes) # Versus option 2: class Containers(): def __init__(self): self.last_selected_scale = "" self.scale_notes = "" self.arpeggionotes = "" v = Container() do_something(v.last_selected_scale) do_another_thing(v.scale_notes, v.arpeggionotes) The two options do *exactly* the same thing, but Option 2 takes much more typing and reading. For what benefit? The only benefit is if you might have multiple Containers at the same time: keyboard_a = Container() keyboard_b = Container() do_something(keyboard_a.last_selected_scale) do_another_thing(keyboard_b.scale_notes, keyboard_b.arpeggionotes) Now you can play and record with two keyboards at the same time. (Perhaps you have four arms? *smile*) In this case, the extra cost of using a class pays for itself. Otherwise, it is just wasted effort. -- 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 Friday, 13 June 2014, 12:45, Steven D'Aprano wrote: 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___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] python3 equivalent of coreutils stat command
With the stat command in GNU coreutils, I can get a file's modification time, with timezone offset. For example, the output of "stat -c %y *" looks like 2014-02-03 14:48:17.0 -0200 2014-05-29 19:00:05.0 -0100 What I want to do is get the mtime in ISO8601 format, and I've gotten close with os.path.getmtime and os.stat, for example 2014-02-03T14:48:17. But, no timezone offset. coreutils stat can get it, so it must be recorded by the filesystem (ext4 in this case). What do I need to do in python to include this piece of information? Thanks ___ 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 14/06/14 13:53, diliup gabadamudalige wrote: Say if I have a lot of Lists, strings and variables used to carry data to and from various functions why can't I have a class with all these in it? You can but it should also have the functions that operate on the data too. Thats the point of classes they link function and data together so you don't need to pass lots of parameters around. If you have a class that jusat holds lots of disparate data items you might as well just use a list/tuple or dictionary. The value of classes is when you add behaviour or methods to the mix. I thought on the lines of blood carrying various things to different organs in the body. But even there the blood has a distinct set of related items, it doesn't carry the food to your stomach or the video signals from your eye to your brain. And blood has operations - it can flow, coagulate, increase/decrease red-cell count etc. It has behaviour as well as data. class Containers(): def __init__(self): self.last_selected_scale= "" self.scale_notes = "" self.arpeggionotes = "" self.handplayed_notes = [] self.MIDIscale = [] # the MIDI scale to be played is here self.play_MIDIscale = False self.play_MIDIarpeggio = False self.play_MIDI_scale_note = False self.play_MIDI_arpeggio_note = False self.MIDInote_inscale = 0 Now that could be a Tune... And it might have record/play/pause operations now say if I play the MIDI keyboard, I collect the notes played which will be appended to the list v.hanplayednotes. This will be done from a function which will scan the keyboard and append played notes to that list. But that could be a method of the Tune sub-class, MidiTune, which knows how to record from a MidiSequencer object. This is how I have used these variables, lists and strings in this program. So far it has run error free. The issue is not how to make it run - you can do that in assembler. The issue is how easy is it to read and how easy os it to modify and fix later. 80% of the cost of commercial software is in "maintenance" The goal of professional software engineers is to reduce maintenance costs even if that increases the initial 20% development budget. Is this not correct or bad design. I know it is not the normal way of using class but is it wrong? Almost certainly, because it adds a layer of complexity for little or no gain. Whereas adding the methods to the class improves the maintainability and readability (and sometimes the performance, although that's a secondary benefit). 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
Re: [Tutor] python3 equivalent of coreutils stat command
On 14/06/14 22:06, street.swee...@mailworks.org wrote: With the stat command in GNU coreutils, I can get a file's modification time, with timezone offset. gotten close with os.path.getmtime and os.stat, for example 2014-02-03T14:48:17. But, no timezone offset. os.stat returns the mtime as seconds from the epoch. The time and datetime modules contain functions for converting seconds into local time etc which can show timezone if needed. But be aware of the following caveat from the documentation: --- Note: The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details. ... --- -- 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