Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: issues with peewee and python

  1. #1
    Join Date
    Jan 2011
    Beans
    78

    issues with peewee and python

    hello dear ubuntu-experts



    just some basics on issues i run into while starting with python..


    well this is somewhat difficult - why do i get these results!!?`

    here the original posting

    fairly new to python and to programming too.

    I'm trying to connect to a MySQL database using peewee and I can't get it to work. I'm new to databases so I'm probably doing something stupid, but this is what I'm trying: well i tried to get connection to a database in python with peewee but at a certain point the programme fails.

    Code:
    import urllib
    import urlparse
    import re
    # import peewee
    import json
    from peewee import *
    #from peewee import MySQLDatabase ('cpan', user='root',passwd='rimbaud') 
    db = MySQLDatabase('cpan', user='root',passwd='rimbaud') 
    
    class User(Model):
        name = TextField()
        cname = TextField()
        email = TextField()
        url = TextField()
    
        class Meta:
            database = db # this model uses the cpan database
    
    User.create_table() #ensure table is created
    
    url = "http://search.cpan.org/author/?W"
    html = urllib.urlopen(url).read()
    for lk, capname, name in re.findall('<a rel="nofollow" href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
        alk = urlparse.urljoin(url, lk)
        data = { 'url':alk, 'name':name, 'cname':capname }
        phtml = urllib.urlopen(alk).read()
        memail = re.search('<a href="mailto:(.*?)">', phtml)
        if memail:
            data['email'] = memail.group(1)
    
    # data = json.load('email') #your json data file here
    for entry in data: #assuming your data is an array of JSON objects
        user = User.create(name=entry["name"], cname=entry["cname"],
            email=entry["email"], url=entry["url"])
        user.save()


    the results:i am faded

    Code:
    martin@linux-70ce:~/perl> python cpan_100.py
    Traceback (most recent call last):
      File "cpan_100.py", line 45, in <module>
        user = User.create(name=entry["name"], cname=entry["cname"],
    TypeError: string indices must be integers, not str
    well at the moment i wonder why i run into there errors
    Last edited by dilbert_one; February 25th, 2015 at 02:31 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    UK
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: issues with peewee and python

    Thread moved to Programming Talk.
    Ubuntu 20.04 Desktop Guide - Ubuntu 22.04 Desktop Guide - Forum Guide to BBCode - Using BBCode code tags

    Member: Not Canonical Team

    If you need help with your forum account, such as SSO login issues, username changes, etc, the correct place to contact an admin is here. Please do not PM me about these matters unless you have been asked to - unsolicited PMs concerning forum accounts will be ignored.

  3. #3
    Join Date
    Oct 2014
    Beans
    19
    Distro
    Ubuntu Studio 14.04 Trusty Tahr

    Re: issues with peewee and python

    As the error says:
    Code:
    TypeError: string indices must be integers, not str
    What you did as a mistake was to extract a value from an array by using a string when you should use an integer instead. Still, that's not the issue.
    Using arrays inside for-loops splits up the array into individual objects that then returns to the array once the loop's done. What you did was treat these objects as arrays, hence the error.
    Ex. "for entry in data:" splits up the array "data" into "entry", which is an object. The loop runs as many times as the size of "data", to apply it to each "entry" inside "data".

    To solve it, you need to replace:
    user = User.create(name=entry["name"], cname=entry["cname"],
    email=entry["email"], url=entry["url"])
    with:
    user = User.create(name=entry, cname=entry,
    email=entry, url=entry)

    I hope that solves your issue.
    - Gustaf

  4. #4
    Join Date
    Jan 2011
    Beans
    78

    Re: issues with peewee and python

    hi gustaf

    many thanks will try it out later the weekend and come back to report all the findings

    again many thanks

  5. #5
    Join Date
    Jan 2011
    Beans
    78

    Re: issues with peewee and python

    hello dear Gustaf

    thx for the answer

    Code:
    import urllib
    import urlparse
    import re
    # import peewee
    import json
    from peewee import *
    #from peewee import MySQLDatabase ('cpan', user='root',passwd='rimbaud') 
    db = MySQLDatabase('cpan', user='root',passwd='rimbaud') 
    
    class User(Model):
        name = TextField()
        cname = TextField()
        email = TextField()
        url = TextField()
    
        class Meta:
            database = db # this model uses the cpan database
    
    User.create_table() #ensure table is created
    
    url = "http://search.cpan.org/author/?W"
    html = urllib.urlopen(url).read()
    for lk, capname, name in re.findall('<a rel="nofollow" href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
        alk = urlparse.urljoin(url, lk)
        data = { 'url':alk, 'name':name, 'cname':capname }
        phtml = urllib.urlopen(alk).read()
        memail = re.search('<a href="mailto:(.*?)">', phtml)
        if memail:
            data['email'] = memail.group(1)
    
    # data = json.load('email') #your json data file here
    for entry in data: #assuming your data is an array of JSON objects
    
    user = User.create(name=entry, cname=entry,
    email=entry, url=entry)
    user.save()
    got back the following

    Code:
    IndentationError: expected an indented block                                                                                                                                        
    martin@linux-70ce:~/perl> python cpan_1.py                                                                                                                                          
      File "cpan_1.py", line 35                                                                                                                                                         
        user = User.create(name=entry, cname=entry,                                                                                                                                     
           ^
    IndentationError: expected an indented block
    martin@linux-70ce:~/perl>
    what can i do?

  6. #6
    Join Date
    Jan 2011
    Beans
    78

    Re: issues with peewee and python

    still stuggle with the errors

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: issues with peewee and python

    Since "user = User.create(...)" appears after a "for entry in data:" it should be indented(*).


    (*) for the rare case where you would want an empty loop, the loop body should just be a "pass" instruction.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  8. #8
    Join Date
    Jan 2011
    Beans
    78

    Smile Re: issues with peewee and python

    hello dear ofnuts

    many many thanks for the quick answer and the hints.

    just not sitting of my Notebook with python on it but want to answer you quickly,


    thanks for pointing into this direction - guess that this wrong indentation caused the issues. - and the error code also mentioned it. Many thanks dear Ofnuts for helping me.


    At the weekend i will try it out.

    greetings
    dil

    "whitspaces in python are significant to the source code."
    Everywhere else, whitespace is not significant and can be used as you like, just like in any other language..... but Python is behaving in a special way...

    you helped me alot...

  9. #9
    Join Date
    Jan 2011
    Beans
    78

    Re: issues with peewee and python

    hello again

    still struggle with the code -

    regarding the data that comes out - we can do like so... : to see the outcome ...

    Code:
    import urllib
    import urlparse
    import re
    
    url = "http://search.cpan.org/author/?W"
    html = urllib.urlopen(url).read()
    for lk, capname, name in re.findall('<a href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
        alk = urlparse.urljoin(url, lk)
    
        data = { 'url':alk, 'name':name, 'cname':capname }
    
        phtml = urllib.urlopen(alk).read()
        memail = re.search('<a href="mailto:(.*?)">', phtml)
        if memail:
            data['email'] = memail.group(1)
    
        print data

    We can see the output here

    Code:
    $ python printer.py
    {'url': 'http://search.cpan.org/~wac/', 'cname': 'WAC', 'name': 'Wang Aocheng', 'email': 'wangaocheng@hotmail.com'}
    {'url': 'http://search.cpan.org/~wade/', 'cname': 'WADE', 'name': 'James Wade', 'email': 'james@holifield.com'}
    ^C
    note: this is only a sample of output - before i did stop it via Ctrl+C


    what do you say - how to do the assignment now....

  10. #10
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: issues with peewee and python

    What you get here is an array of dictionaries, each user corresponding to a dictionary. You can get at each user data with:
    Code:
    result[userIndex][filedName]
    For instance:
    Code:
    # Yields 'WADE'
    cname=result[1]['cname']
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •