Re: [Tutor] How to create an object in database only if the object is not already there?
From: Tutoron behalf of GMX Sent: Monday, September 11, 2017 9:38 AM To: tutor@python.org Subject: [Tutor] How to create an object in database only if the object is not already there? Now when I create a Course object like this: >>> Course(title=‘A new course’) An object is create in the database. I don’t want to have this behaviour, but what I want to be doing is create a Course object and then only commit in the database if the course is not already available in the database. ===> I have no experience with Pony, but with Sqlalchemy one could do something like: if not session.query(Course.title).filter_by(title=‘A new course’).first(): course = Course(title=‘A new course’) Or perhaps you could set a primary key constraint on course title. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about calendar module in standard libriary
Айнур Зулькарнаев wrote: > Hello all! > > > There is a class Calendar in calendar.py in standard libriary. > > > class Calendar(object): > """ > Base calendar class. This class doesn't do any formatting. It > simply provides data to subclasses. > """ > > def __init__(self, firstweekday=0): > self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday > > def getfirstweekday(self): > return self._firstweekday % 7 > > def setfirstweekday(self, firstweekday): > self._firstweekday = firstweekday > > firstweekday = property(getfirstweekday, setfirstweekday) > > > As far as I understand, even if user enters inappropriate firstweekday > parameter (bigger than 6) during instansiation of the Calendar, the > Calendar swallows it (and latter returns correct firstweekday value due to > %7 in getfirstweekday method). > > > So, the question is why not explicitly raise ValueError if user enters the > firstweekday parameter bigger that 6 (with accordance with the Zen). Am I > missing something? It does no harm to those who use the class properly while it allows those unfamiliar with the idea of a 0th day to specify 7 instead 0. The behaviour thus may be interpreted as an example of """Be strict when sending and tolerant when receiving.""" See also https://tools.ietf.org/html/rfc1958 https://en.wikipedia.org/wiki/Robustness_principle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about calendar module in standard libriary
On Mon, Sep 11, 2017 at 10:58:51AM +, Айнур Зулькарнаев wrote: > class Calendar(object): > def __init__(self, firstweekday=0): > self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday > > def getfirstweekday(self): > return self._firstweekday % 7 > > def setfirstweekday(self, firstweekday): > self._firstweekday = firstweekday > > firstweekday = property(getfirstweekday, setfirstweekday) > > As far as I understand, even if user enters inappropriate firstweekday > parameter (bigger than 6) during instansiation of the Calendar, the > Calendar swallows it (and latter returns correct firstweekday value > due to %7 in getfirstweekday method). That looks right to me. > So, the question is why not explicitly raise ValueError if user enters > the firstweekday parameter bigger that 6 (with accordance with the > Zen). Am I missing something? I don't think there is any specific reason. Probably just the personal choice of the person who wrote the code. The decision doesn't appear to be documented anywhere, so I don't think its official behaviour. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about calendar module in standard libriary
On 11/09/17 11:58, Айнур Зулькарнаев wrote: > So, the question is why not explicitly raise ValueError if > user enters the firstweekday parameter bigger that 6 Its a valid question but you probably need to find the original PEP document to find the answer and that module has been around for a *very* long time! -- 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 to create an object in database only if the object is not already there?
On 11/09/17 10:38, GMX wrote: > I am using `pony` orm to write a simple class as my model. pony is not a standard module (indeed I only heard about it from your email) so you are probably best asking on the pony support forum if such exists or via the author. > from pony.orm import Database > from pony.orm import Required, Optional > from pony.orm import db_session > from pony.orm import select, commit > > DB_PATH = ‘db.sqlite’ > > db = Database() > > class Course(db.Entity): > """ > A class to represent a course > """ > title = Required(str, unique=True) > url = Optional(str, unique=True) > thumbnail = Optional(str) > processed = Optional(bool, default=False) > > db.bind(provider='sqlite', filename=DB_PATH, create_db=True) > db.generate_mapping(create_tables=True) > ``` > > Now when I create a Course object like this: > > >>> Course(title=‘A new course’) > > An object is create in the database. I don’t want to have this behaviour, But that's kind of what an ORM does. The whole point of using an ORM is that it translates your objects into database transactions. > but what I want to be doing is create a Course object > and then only commit in the database if the course is > not already available > in the database. Thats very specific to your ORM. There probably are options to do that, but you will need to ask the pony users. -- 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] question about calendar module in standard libriary
Hello all! There is a class Calendar in calendar.py in standard libriary. class Calendar(object): """ Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses. """ def __init__(self, firstweekday=0): self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday def getfirstweekday(self): return self._firstweekday % 7 def setfirstweekday(self, firstweekday): self._firstweekday = firstweekday firstweekday = property(getfirstweekday, setfirstweekday) As far as I understand, even if user enters inappropriate firstweekday parameter (bigger than 6) during instansiation of the Calendar, the Calendar swallows it (and latter returns correct firstweekday value due to %7 in getfirstweekday method). So, the question is why not explicitly raise ValueError if user enters the firstweekday parameter bigger that 6 (with accordance with the Zen). Am I missing something? Thank you in advance Best regards, Aynur С уважением, А.И. Зулькарнаев ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to create an object in database only if the object is not already there?
Hi, I am using `pony` orm to write a simple class as my model. Here is the class. ``` from pony.orm import Database from pony.orm import Required, Optional from pony.orm import db_session from pony.orm import select, commit DB_PATH = ‘db.sqlite’ db = Database() class Course(db.Entity): """ A class to represent a course """ title = Required(str, unique=True) url = Optional(str, unique=True) thumbnail = Optional(str) processed = Optional(bool, default=False) db.bind(provider='sqlite', filename=DB_PATH, create_db=True) db.generate_mapping(create_tables=True) ``` Now when I create a Course object like this: >>> Course(title=‘A new course’) An object is create in the database. I don’t want to have this behaviour, but what I want to be doing is create a Course object and then only commit in the database if the course is not already available in the database. If the course is already in the database, the orm should just return the same object. I have implemented the requirement as follows: ``` class Course(db.Entity): """ A class to represent a course """ title = Required(str, unique=True) url = Optional(str, unique=True) thumbnail = Optional(str) processed = Optional(bool, default=False) @staticmethod @db_session def new(title, url=None, thumbnail=None, processed=False): """Return a Course either new or from database""" course = select(c for c in Course if c.title == title)[:] if course: return course[0] return Course(title=title, url=url, thumbnail=thumbnail, processed=processed) db.bind(provider='sqlite', filename=DB_PATH, create_db=True) db.generate_mapping(create_tables=True) ``` Can there be a better method of doing that? Please let me know. Thanks. “ You are not born knowing everything. You go on learning” - Anubhav Yadav ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor