How to create an object in database only if the object is not already there?

2017-09-11 Thread GMX
Hi, 

Reposting again to the list as I had made a silly mistake in typing the subject 
line last week. 

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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python - requests - forms - web scraping - how to deal with multi stage forms

2018-05-17 Thread GMX

https://stackoverflow.com/questions/50383210/python-requests-how-to-post-few-stages-forms
 
-- 
https://mail.python.org/mailman/listinfo/python-list 


Hi, 

Your post will get more traction on the email list if you try to post the 
actual question on the list against posting a url to an external website for 
your question. 

Coming back to the question. I think what you want to do is implement some 
client side processing using Javascript or one of it’s frameworks and implement 
the parts. Once everything is done you can submit it to the python server. 

Hope that helps. 



Anubhav.
-- 
https://mail.python.org/mailman/listinfo/python-list


JS3550 PYTHON DEVELOPER Contract Antwerp 4 months initial

2005-03-23 Thread bob gmx
Dear All,

is anyone interested in this Python vacancy:-

WWW.johnstout.co.uk

JS3550  PYTHON DEVELOPERContract Antwerp  4 months initial

Our client is a major organisation in Antwerp. They have an urgent requirement 
for a specialist developer with good knowledge of Python and C++

Job description: :-He will be responsible for the integration of models 
developed 
in C of R with software MX .,The integration will be handled via the scripting 
language Python .Technical Competence required: Java, C++, Python
 
Knowledge and experience with API 

If you are interested and would like to find out more about these positions 
please 
contact me by telephone or by EMAIL.

John Stout
020-8308-0555
[EMAIL PROTECTED]

__

Gesendet von Yahoo! Mail - http://mail.yahoo.de
Bis zu 100 MB Speicher bei http://premiummail.yahoo.de
-- 
http://mail.python.org/mailman/listinfo/python-list