Re: [Tutor] value range checker
Albert-Jan Roskam wrote: > I have a written a function checks the validity of values. The ranges of > valid values are stored in a database table. > > Such a table contains three columns: category, min and max. One record of > such a table specifies the range for > > a certain category, but a category may be spread out over multiple > records. > My questions: > def get_valid_value_lookup(records): > """ > Translates value range information (from a database table) > into a dictionary of the form {: [ values>]} """ > Boundaries = collections.namedtuple("Boundaries", "category min max") > records = [Boundaries(*record) for record in records] > boundaries = collections.defaultdict(list) > crap = [boundaries[record.category].__iadd__(range(record.min, > record.max + 1)) > for record in records] > return dict(boundaries) > [1] @ is_valid: is there a better way to do this? I mostly don't like the > [use of the __iadd__ dunder method. When you find yourself using a list comprehension for its side effect it is high time to switch to a for loop. So def get_valid_value_lookup(records): boundaries = {} for category, minval, maxval in records: boundaries.setdefault(category, []).extend(range(minval, maxval+1)) return boundaries > [2] @ is_valid2: Perhaps an answer to my previous question. Is this a > [better approach? As long as the size of the ranges is manageable and you are not actually seeing float values I'd stick with the dead simple first version. Replace the list in get_valid_value_lookup() with a set boundaries.setdefault(category, set()).update(range(minval, maxval+1)) and you get O(1) lookup for free. > def is_valid2(lookup, category, value): > """Return True if value is member of a list of a given category, False > otherwise.""" > # this version also knows how to deal with floats. > > try: > > L = lookup[category] > except KeyError: > raise KeyError("Invalid category: %r" % category) > > adjusted_value = value if int(value) in (L[0], 0, L[-1]) else > math.ceil(value) > try: > chopfunc = bisect.bisect_right if value < L[0] else > bisect.bisect_left return L[chopfunc(L, value)] == adjusted_value > except IndexError: > return False I don't understand what this is supposed to do: (1) for bisect to work correctly the list has to be sorted. Your get_valid_value_lookup() doesn't do that (2) Why/how are L[0], 0, and L[-1] special? (3) Given a sorted list L there should be no need to bisect_whatever for value < L[0] > [3] I am inheriting a this system. It feels a bit strange that these range > [check values are stored in a database. > > Would yaml be a better choice? Some of the tables are close to 200 > records. Unless you're encountering an actual inconvenience just keep it like it is. Yea, I'm a lazy bastard ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How should my code handle db connections? Should my db manager module use OOP?
boB Stepp wrote: > My ongoing project will be centered around an SQLite db. Since almost > all data needed by the program will be stored in this db, my thought > is that I should create a connection to this db shortly after program > startup and keep this connection open until program closure. I am > assuming that opening and closing a db connection has enough overhead > that I should only do this once. But I do not *know* that this is > true. Is it? If not, then the alternative would make more sense, > i.e., open and close the db as needed. > > In the first iteration of my project, my intent is to create and > populate the db with tables external to the program. The program will > only add entries to tables, query the db, etc. That is, the structure > of the db will be pre-set outside of the program, and the program will > only deal with data interactions with the db. My intent is to make > the overall design of the program OO, but I am wondering how to handle > the db manager module. Should I go OO here as well? With each > pertinent method handling a very specific means of interacting with > the db? Or go a procedural route with functions similar to the > aforementioned methods? It is not clear to me that OOP provides a > real benefit here, but, then again, I am learning how to OOP during > this project as well, so I don't have enough knowledge yet to > realistically answer this question. Don't overthink your project. However thorough your preparations you will get about 50 percent of your decisions wrong. Use version control to keep track of your code and don't be afraid to throw parts away that don't work out. Implement a backup scheme for the database lest you annoy your wife by making her reenter data, and there you go. The database will be small for a long time, so it will be practical to make a copy every day. Regarding database access: (1) A single connection: _db = None @contextmanager def open_db(): global _db if _db is None: _db = sqlite3.connect(...) is_owner = True else: is_owner = False try: yield _db finally: if is_owner: _db.close() (2) Multiple connections: @contextmanager def open_db(): db = sqlite3.connect(...) try: yield db finally: db.close() You can use both the same way you deal with an open file: with open_db() as db: ... I don't say you should use the above code, I just want to demonstrate that you can happily defer the answer to your connections question. Regarding OO design in general: keep your classes small. You can't go wrong with fewer, smaller and more general methods ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How should my code handle db connections? Should my db manager module use OOP?
On 27/08/15 01:11, boB Stepp wrote: My ongoing project will be centered around an SQLite db. Since almost all data needed by the program will be stored in this db, my thought is that I should create a connection to this db shortly after program startup and keep this connection open until program closure. That's the usual approach with Sqlite. Remember it is just a single file so as soon as you open it it is locked so other users can't access it. But that's not going to be a problem for your app, at least in the early days. Of course keeping any file open for extended periods carries a risk of corruption so you may want to implement an auto store/copy regime so that there is always a recent backup. But if the app is only being used for a few minutes at a time then it might be overkill. assuming that opening and closing a db connection has enough overhead that I should only do this once. But I do not *know* that this is true. Is it? Its a good habit to get into. In fact Sqlite doesn't take too much work to open because its just a file but once you get into server databases its a much bigger overhead. So I'd just treat Sqlite as another database in that regard. In the first iteration of my project, my intent is to create and populate the db with tables external to the program. The program will only add entries to tables, query the db, etc. That is, the structure of the db will be pre-set outside of the program, and the program will only deal with data interactions with the db. Yes, that makes sense. the overall design of the program OO, but I am wondering how to handle the db manager module. Should I go OO here as well? I'm not clear what exactly you see the db manager module doing? Is this the admin module? The data loader that sits outside the app? Or a module within the app used by the objects? For admin (assuming a CLI) I'd personally stick with procedural. For data loader I'd stick with procedural and pure SQL. For the main app I'd build a very thin procedural API over the individual SQL queries and then let each model class handle its own data access via that API. The API is then all you need to change if that database changes. OR go with a third party ORM (more to learn for little gain IMHO). hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How should my code handle db connections? Should my db manager module use OOP?
On Wed, Aug 26, 2015 at 10:22:25PM -0500, Zachary Ware wrote: > On Aug 26, 2015 9:03 PM, "Steven D'Aprano" wrote: > > - If your database lives on a NTFS partition, which is very common for > > Linux/Unix users > > > these issues, especially on Linux when using NTFS. > > Surely you mean NFS, as in Network FileSystem, rather than NTFS as in New > Technology FileSystem? :) Indeed I do, thank you for the correction, and apologies for the confusion. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] tkinter in Python 3
I'm trying to move a Python 2.x program to Python 3.x. When I try to import tkinter I get the error message that no module _tkinter can be found. I've tried sudo apt-get install python-tk. While this command works, there is no difference in the result. This problem has only cropped up after I changed to Ubuntu 15.04. Does anyone have any suggestions? Thanks in advance for any help. Mike ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tkinter in Python 3
I am trying to download a version of Pygame that is compatible with Python 2.7.10; when I try to run a simple program, I get an error message stating that terms like "livewires", "Sprite", "pygame", and "screen", are unresolved references, and so I assume that downloading pygame could solve this problem. However, even after I downloaded it, the same messages still appear. Might anyone have any alternatives or suggestions? Thank you very much, Rohan On Thu, Aug 27, 2015 at 1:21 PM, Michael Thomas wrote: > I'm trying to move a Python 2.x program to Python 3.x. When I try to import > tkinter I get the error message that no module _tkinter can be found. I've > tried sudo apt-get install python-tk. While this command works, there is no > difference in the result. This problem has only cropped up after I changed > to Ubuntu 15.04. Does anyone have any suggestions? > Thanks in advance for any help. > Mike > ___ > 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] tkinter in Python 3
On 27/08/15 18:21, Michael Thomas wrote: I'm trying to move a Python 2.x program to Python 3.x. When I try to import tkinter I get the error message that no module _tkinter can be found. I've tried sudo apt-get install python-tk. Thats the Python2 version Try apt-get install python3-tk Note that there are a lot of changes in Tkinter in Python 3, the modules are drastically rearranged and renamed. Good luck. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Pygame in v2 was: Re: tkinter in Python 3
On 27/08/15 22:29, Rohan S wrote: I am trying to download a version of Pygame that is compatible with Python 2.7.10; While a similar problem it would have been better to start a new thread. These are quite different problems. However... when I try to run a simple program, I get an error message stating that terms like "livewires", "Sprite", "pygame", and "screen", are unresolved references, and so I assume that downloading pygame could solve this problem. Maybe, but the mention of Livewires suggests you may be following the Michael Dawson book? If so he has download instructions in the book for a specific copy of Livewires that matches the code examples. I recommend you use the version he suggests. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor