Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58512 > unrolled thread
| Started by | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-11-05 19:06 +0200 |
| Last post | 2013-11-05 19:32 -0500 |
| Articles | 20 on this page of 33 — 16 participants |
Back to article view | Back to comp.lang.python
Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 19:06 +0200
Re: Help me with this code PLEASE Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 18:23 +0100
Re: Help me with this code PLEASE mm0fmf <none@mailinator.com> - 2013-11-05 17:33 +0000
Re: Help me with this code PLEASE Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-05 17:41 +0000
Re: Help me with this code PLEASE Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-05 18:53 +0100
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 19:59 +0200
Re: Help me with this code PLEASE Tobiah <toby@tobiah.org> - 2013-11-05 10:11 -0800
Re: Help me with this code PLEASE Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-05 14:01 -0500
Re: Help me with this code PLEASE Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-05 18:02 +0000
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 20:09 +0200
Re: Help me with this code PLEASE Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-05 20:10 +0000
Re: Help me with this code PLEASE Cameron Simpson <cs@zip.com.au> - 2013-11-06 09:31 +1100
Re: Help me with this code PLEASE bob gailer <bgailer@gmail.com> - 2013-11-05 18:15 -0500
Re: Help me with this code PLEASE Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-05 23:36 +0000
Re: Help me with this code PLEASE John Gordon <gordon@panix.com> - 2013-11-05 20:19 +0000
Re: Help me with this code PLEASE Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-05 21:17 +0000
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-05 23:26 +0200
Re: Help me with this code PLEASE John Gordon <gordon@panix.com> - 2013-11-05 22:06 +0000
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-06 00:28 +0200
Re: Help me with this code PLEASE Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-05 22:44 +0000
Re: Help me with this code PLEASE John Gordon <gordon@panix.com> - 2013-11-05 22:54 +0000
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-06 03:14 +0200
Re: Help me with this code PLEASE Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-06 08:49 +0000
Re: Help me with this code PLEASE Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-06 08:57 -0500
Re: Help me with this code PLEASE Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-06 09:27 +0100
Re: Help me with this code PLEASE Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-05 21:42 +0000
Re: Help me with this code PLEASE Chris Angelico <rosuav@gmail.com> - 2013-11-06 09:03 +1100
Re: Help me with this code PLEASE Piet van Oostrum <piet@vanoostrum.org> - 2013-11-05 18:15 -0400
Re: Help me with this code PLEASE Nick the Gr33k <nikos.gr33k@gmail.com> - 2013-11-06 00:35 +0200
Re: Help me with this code PLEASE Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-06 00:45 +0000
Re: Help me with this code PLEASE Ben Finney <ben+python@benfinney.id.au> - 2013-11-06 12:46 +1100
Re: Help me with this code PLEASE Grant Edwards <invalid@invalid.invalid> - 2013-11-06 14:50 +0000
Re: Help me with this code PLEASE Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-05 19:32 -0500
Page 1 of 2 [1] 2 Next page →
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 19:06 +0200 |
| Subject | Help me with this code PLEASE |
| Message-ID | <l5b8if$4k1$1@dont-email.me> |
======================================
# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()
ref = visit = download = []
if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data
# retrieve long strings and convert them into lists respectively
ref = ref.split()
visit = visit.split()
download = download.split()
else:
# initiate these values
ref = ref
visit = lastvisit
download = ''
refs = visits = downloads = []
# add current values to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )
# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )
# save this visit as an entry into database
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE refs = %s, visits = %s, hits = hits + 1,
downloads = %s''',
(cID, refs, host, city, useros, browser, visits, downloads, refs,
visits, downloads) )
=============================================
IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.
ALL I WANT IT TO DO IS JUST
1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
PYTHON DATATYPE TO MYSQL SCALAR STRING.
EVERYHTIGN I TRIED FAILED.
[toc] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2013-11-05 18:23 +0100 |
| Message-ID | <mailman.2058.1383672199.18130.python-list@python.org> |
| In reply to | #58512 |
> ============================================= > > IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK. > > ALL I WANT IT TO DO IS JUST > > 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS > 2. CONVERT LONG STRINGS TO LISTS > 3. ADD SOME CURRENT VALUES TO THOSE LISTS > 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR STRING. > > EVERYHTIGN I TRIED FAILED. Don't start a new thread, where you just ask the same question, you already asked before. The answer will not be different just because you started a new thread. Did you already read the documentation of fetchone? -- Antoon Pardon
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2013-11-05 17:33 +0000 |
| Message-ID | <QZ9eu.8310$Mn5.3603@fx25.am4> |
| In reply to | #58512 |
> EVERYHTIGN I TRIED FAILED. Maybe try some of the advice you have been given instead?
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-05 17:41 +0000 |
| Message-ID | <52792de4$0$29979$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #58512 |
On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote: > ALL I WANT IT TO DO IS JUST > > 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG > STRINGS TO LISTS > 3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG > STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR > STRING. You cannot do this with Python. You should use a better language more suited to you. Try this: http://php.net/manual/en/tutorial.php I hope that you will be much happier with this, since you are struggling to get Python to work the way you want it to. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2013-11-05 18:53 +0100 |
| Message-ID | <mailman.2059.1383674042.18130.python-list@python.org> |
| In reply to | #58515 |
Op 05-11-13 18:41, Steven D'Aprano schreef: > On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote: > >> ALL I WANT IT TO DO IS JUST >> >> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG >> STRINGS TO LISTS >> 3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG >> STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR >> STRING. > > > You cannot do this with Python. You should use a better language more > suited to you. Try this: > > http://php.net/manual/en/tutorial.php > > > I hope that you will be much happier with this, since you are struggling > to get Python to work the way you want it to. Steve, what went on in your mind? If Nikos follows you advise he will probably come here with all kinds of php questions. -- Antoon Pardon
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 19:59 +0200 |
| Message-ID | <l5bblo$o04$1@dont-email.me> |
| In reply to | #58515 |
Στις 5/11/2013 7:41 μμ, ο/η Steven D'Aprano έγραψε: > On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote: > >> ALL I WANT IT TO DO IS JUST >> >> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG >> STRINGS TO LISTS >> 3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG >> STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR >> STRING. > > > You cannot do this with Python. You should use a better language more > suited to you. Try this: > > http://php.net/manual/en/tutorial.php > > > I hope that you will be much happier with this, since you are struggling > to get Python to work the way you want it to. > > -- Steven, i want switch to php, i uses to start with php, then perl and now i ended up with python. All this problem arises because MySQL's hasn't got a datatype able to store an array of elements, a list. Is there some other database, sql connector i can use except 'pymysql' that supports a "collection" record type? postgresql or somethign similar? If it does i will switch to that and avoid these tedius convertions from long strings => list and backwise. Somehting that can just take Python's datatypes 'list' or 'tuple' or 'dict' as tehy are and just store them into the database convertionless.
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2013-11-05 10:11 -0800 |
| Message-ID | <Ixaeu.86369$9P6.50810@fx22.iad> |
| In reply to | #58518 |
All this problem arises because MySQL's hasn't got a datatype able to store an array of elements, a list. Um, yes it does. It's called a table.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-11-05 14:01 -0500 |
| Message-ID | <mailman.2060.1383678083.18130.python-list@python.org> |
| In reply to | #58521 |
On Tue, Nov 5, 2013 at 1:11 PM, Tobiah <toby@tobiah.org> wrote: > All this problem arises because MySQL's hasn't got a datatype able to store > an array of elements, a list. > > Um, yes it does. It's called a table. > -- > https://mail.python.org/mailman/listinfo/python-list Perhaps we are splitting hairs, but a relational database will let you stuff any text you want in a text field, but First Normal Form is a necessary (and not complete) requirement (from wikipedia) First normal form (1NF) is a property of a relation in a relational database. A relation is in first normal form if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain.[1] If you don't have a single thing in a field, you can't search or sort it or filter by its value with sql. Since those are very important properties of sql, not using 1nf is similar to tossing a whole warehouse of stuff in a warehouse without any sort of organization for where different things are to be put, and therefore, where they can be found. If you don't have first normal form data, you are misusing a relational database. If you don't want to learn about relational databases and what they can do to help you construct software that solves interesting problems, then perhaps you are not curious enough to ever become competent in the field. That said, sql is a different beast than is python or php or any procedural (or oops) language. Just like writing HTML and CSS is something that people who write computer programs for the web might do, its a total shift in thinking from writing python. There is no rule that a person must be a computer programmer, but if a person wants to be a competent and successful computer programmer, one must learn from the evolving understanding of the last 50 years or so. Its a craft of understanding how to divide impossibly large problems into understandable pieces -- using the best tools for each piece. You may say this is just one person's opinion -- but then again, I'm not the one screaming in all caps about the same several problems over and over for the last year or more on this list. The results show no more understanding or growth in skills, and an attitude that never shows a glimmer of interest in learning. -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-05 18:02 +0000 |
| Message-ID | <l5bbrp$fbc$2@dont-email.me> |
| In reply to | #58512 |
On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote: > IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK. Try starting with something simple. The following is a step by step guide to working out how you need to do this. Follow all the steps. Do not skip any steps. Each stage builds on the previous code. Write python code to create a list of strings and print out the elements of the list. This just needs to generate a list of n entries of m characters in length of random character data. Write python code to concatenate a list of strings into some longer string using a separator and print out the long string. Now add some python code to split the long string into a list based on the separator, and print out the list elements. Once you have this code working, make a test table with a single string column and an integer index field in a test database. Now this is starting to push the boundaries of your abilities, but write code to create two lists, and store them with indexes 1 and 2 respectively. Now, and this is really really going to tax you, write some more code that will first retrieve the string for index 1, convert it back into a list, and display the list contents, then do the same for list 2, then retrieve all the data from the db and reproduce each list in turn. Then you might be ready to try coding what you're trying to code. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 20:09 +0200 |
| Message-ID | <l5bc94$roa$1@dont-email.me> |
| In reply to | #58519 |
Στις 5/11/2013 8:02 μμ, ο/η Denis McMahon έγραψε: > On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote: > >> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK. > > Try starting with something simple. The following is a step by step guide > to working out how you need to do this. Follow all the steps. Do not skip > any steps. Each stage builds on the previous code. > > Write python code to create a list of strings and print out the elements > of the list. This just needs to generate a list of n entries of m > characters in length of random character data. > > Write python code to concatenate a list of strings into some longer > string using a separator and print out the long string. > > Now add some python code to split the long string into a list based on > the separator, and print out the list elements. > > Once you have this code working, make a test table with a single string > column and an integer index field in a test database. > > Now this is starting to push the boundaries of your abilities, but write > code to create two lists, and store them with indexes 1 and 2 > respectively. > > Now, and this is really really going to tax you, write some more code > that will first retrieve the string for index 1, convert it back into a > list, and display the list contents, then do the same for list 2, then > retrieve all the data from the db and reproduce each list in turn. > > Then you might be ready to try coding what you're trying to code. > -- Denis, i have already provided my code trying to do what i need and i need some commendation on how to make it work. O even better an rdbms than allows complex data such as tuples, lists, dicts to be saved into the db as they are so i dont have to cobvet back and forth each time.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-11-05 20:10 +0000 |
| Message-ID | <l5bjb1$7cu$1@dont-email.me> |
| In reply to | #58520 |
On Tue, 05 Nov 2013 20:09:42 +0200, Nick the Gr33k wrote:
> Denis, i have already provided my code trying to do what i need and i
> need some commendation on how to make it work.
Nick, you're obviously trying to code way above your abilities.
If you want me to write your code, you will have to pay my day rate, and
you can't afford it. If you could afford it, you'd be paying someone
competent already.
Here, have a code example, this works, although the formatting might get
broken. The database "ntg1" has a single table called str_idx defined as
follows:
create table idx_str ( idx int primary key, str varchar[1024] );
#!/usr/bin/python
import random
import sqlite3
def make_list( length ):
l = []
if ( length < 1 ):
return l
for count in range( 0, length ):
s = '';
for i in range( 1, random.randrange( 4, 12 ) ):
c = chr( random.randrange( 97, 123 ) )
s += c
l.append( s )
return l
def list_to_string( l ):
return "|".join( l )
def string_to_list( s ):
return s.split( "|" )
l1 = make_list( 10 )
print "l1 -> ", l1
s = list_to_string( l1 )
print "s -> ", s
l2 = string_to_list( s )
print "l2 -> ", l2
print "l1 == l2 -> ", l2 == l1
l2 = make_list( 10 )
l3 = make_list( 10 )
l4 = make_list( 10 )
print "Original Lists"
print "l1 -> ", l1
print "l2 -> ", l2
print "l3 -> ", l3
print "l4 -> ", l4
conn = sqlite3.connect( "ntg1" )
cur = conn.cursor()
cur.execute( "delete from idx_str where idx is not null" )
cur.execute(
"insert into idx_str values ( 1, '{0}' )".format(
list_to_string( l1 ) ) )
cur.execute(
"insert into idx_str values ( 2, '{0}' )".format(
list_to_string( l2 ) ) )
cur.execute(
"insert into idx_str values ( 3, '{0}' )".format(
list_to_string( l3 ) ) )
cur.execute(
"insert into idx_str values ( 4, '{0}' )".format(
list_to_string( l4 ) ) )
conn.commit()
conn.close()
print "Lists now in DB"
print "Reading 1 record at a time"
conn2 = sqlite3.connect( "ntg1" )
cur2 = conn2.cursor()
cur2.execute( "select * from idx_str where idx = 1" );
row = cur2.fetchone()
print "stored 1 -> ", row[ 1 ]
print "l1 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 2" );
row = cur2.fetchone()
print "stored 2 -> ", row[ 1 ]
print "l2 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 3" );
row = cur2.fetchone()
print "stored 3 -> ", row[ 1 ]
print "l3 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 4" );
row = cur2.fetchone()
print "stored 4 -> ", row[ 1 ]
print "l4 -> ", string_to_list( row[ 1 ] )
conn2.close()
print "Reading all records at once"
conn3 = sqlite3.connect( "ntg1" )
cur3 = conn3.cursor()
cur3.execute( "select * from idx_str" );
row = cur3.fetchone()
while not row == None:
print "stored ", row[ 0 ], " -> ", row[ 1 ]
print "list ", row[ 0 ], " -> ", string_to_list( row[ 1 ] )
row = cur3.fetchone()
conn3.close()
One thing you haven't considered, what happens if a user has so many
downloads in his list that the converted list doesn't fit in the declared
string column width in your database?
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2013-11-06 09:31 +1100 |
| Message-ID | <mailman.2065.1383692283.18130.python-list@python.org> |
| In reply to | #58520 |
On 05Nov2013 20:09, Nikos <nikos.gr33k@gmail.com> wrote: > O even better an rdbms than allows complex data such as tuples, > lists, dicts to be saved into the db as they are so i dont have to > cobvet back and forth each time. If you're just using the db for storage or adhoc and arbitrary python objects (and not querying the stored values via SQL - eg WHERE), then: make a column of type BLOB, convert Python values to bytes using pickle, store. And of course the reverse. It is not a great use of an RDB, but it seems to adhere to what you ask. -- Cameron Simpson <cs@zip.com.au>
[toc] | [prev] | [next] | [standalone]
| From | bob gailer <bgailer@gmail.com> |
|---|---|
| Date | 2013-11-05 18:15 -0500 |
| Message-ID | <mailman.2066.1383693356.18130.python-list@python.org> |
| In reply to | #58520 |
There is also the shelve module. It uses pickle to marshal a Python object, then stores it in a file under a key. Sample code from the module documentation: import shelve d = shelve.open(filename) # open -- file may get suffix added by low-level library d[key] = data # store data at key (overwrites old data if using an existing key) data = d[key] # retrieve a COPY of data at key (raise KeyError if no such key) del d[key] # delete data stored at key (raises KeyError if no such key) flag = key in d # true if the key exists klist = list(d.keys()) # a list of all existing keys (slow!) -- Bob Gailer 919-636-4239 Chapel Hill NC
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-05 23:36 +0000 |
| Message-ID | <mailman.2067.1383694607.18130.python-list@python.org> |
| In reply to | #58520 |
On 05/11/2013 22:31, Cameron Simpson wrote: > On 05Nov2013 20:09, Nikos <nikos.gr33k@gmail.com> wrote: >> O even better an rdbms than allows complex data such as tuples, >> lists, dicts to be saved into the db as they are so i dont have to >> cobvet back and forth each time. > > If you're just using the db for storage or adhoc and arbitrary > python objects (and not querying the stored values via SQL - eg > WHERE), then: make a column of type BLOB, convert Python values to > bytes using pickle, store. And of course the reverse. > > It is not a great use of an RDB, but it seems to adhere to what you ask. > How do expect the OP to understand a BLOB or pickle or chutney when he doesn't understand why he can't iterate around a Nonetype object? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-11-05 20:19 +0000 |
| Message-ID | <l5bjs6$84r$1@reader1.panix.com> |
| In reply to | #58512 |
In <l5b8if$4k1$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com> writes: > IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK. > ALL I WANT IT TO DO IS JUST > 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS > 2. CONVERT LONG STRINGS TO LISTS > 3. ADD SOME CURRENT VALUES TO THOSE LISTS > 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST > PYTHON DATATYPE TO MYSQL SCALAR STRING. > EVERYHTIGN I TRIED FAILED. How did it fail? Error message? No results at all? Different results than you wanted? If so, how did they differ? -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-05 21:17 +0000 |
| Message-ID | <mailman.2061.1383686285.18130.python-list@python.org> |
| In reply to | #58524 |
On 05/11/2013 20:19, John Gordon wrote: > In <l5b8if$4k1$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com> writes: > >> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK. > >> ALL I WANT IT TO DO IS JUST > >> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS >> 2. CONVERT LONG STRINGS TO LISTS >> 3. ADD SOME CURRENT VALUES TO THOSE LISTS >> 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST >> PYTHON DATATYPE TO MYSQL SCALAR STRING. > >> EVERYHTIGN I TRIED FAILED. > > How did it fail? > > Error message? > No results at all? > Different results than you wanted? If so, how did they differ? > Dear John, This approach has already been tried by Dave Angel, Steven D'Aprano and Chris Angelico amongst others and has failed dismally so why should you be any different? I've taken a different approach. I've put the contract out to tender and hereby give you the winners http://www.mudefordwoodcommunitycentre.co.uk/playgroup-and-tiny-tots/ Part of the rationale is that they're literally 100 yards from my front door so communicating with them effectively should be extremely easy. This will be particularly useful when it comes down to debugging such difficult issues as "> TypeError: 'NoneType' object is not iterable". I'm looking forward to working in partnership with them and am convinced that within a few days all of Nikos' problems will have been solved. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-05 23:26 +0200 |
| Message-ID | <l5bnpc$71v$1@dont-email.me> |
| In reply to | #58524 |
Στις 5/11/2013 10:19 μμ, ο/η John Gordon έγραψε:
> In <l5b8if$4k1$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com> writes:
>
>> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.
>
>> ALL I WANT IT TO DO IS JUST
>
>> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
>> 2. CONVERT LONG STRINGS TO LISTS
>> 3. ADD SOME CURRENT VALUES TO THOSE LISTS
>> 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
>> PYTHON DATATYPE TO MYSQL SCALAR STRING.
>
>> EVERYHTIGN I TRIED FAILED.
>
> How did it fail?
>
> Error message?
> No results at all?
> Different results than you wanted? If so, how did they differ?
>
I know i'm close to solution, i can feel it but i have some issues.
The code we arr discussing is the following:
=================================================================================================================
# ~ DATABASE INSERTS ~
=================================================================================================================
if cookieID != 'nikos' and ( os.path.exists( path + page ) or
os.path.exists( cgi_path + page ) ) and re.search(
r'(amazon|google|proxy|cloud|reverse|fetch|msn|who|spider|crawl|ping)',
host ) is None:
try:
# if first time for webpage; create new record( primary key is
automatic, hit is defaulted ), if page exists then update record
cur.execute('''INSERT INTO counters (url) VALUES (%s) ON DUPLICATE KEY
UPDATE hits = hits + 1''', page )
cID = cur.lastrowid
# fetch those columns that act as lists but are stored as strings
cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()
ref = visit = download = []
if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data
# retrieve long strings and convert them into lists respectively
ref = ref.split()
visit = visit.split()
download = download.split()
else:
# initiate these values
ref = ref
visit = lastvisit
download = ''
refs = visits = downloads = []
# add current values to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )
# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )
# save this visit as an entry into database
cur.execute('''INSERT INTO visitors (counterID, refs, host, city,
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE refs = %s, visits = %s, hits = hits + 1,
downloads = %s''',
(cID, refs, host, city, useros, browser, visits, downloads, refs,
visits, downloads) )
con.commit()
except pymysql.ProgrammingError as e:
print( repr(e) )
con.rollback()
sys.exit(0)
===============================================
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 274, in <module>
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
visit.split()
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
AttributeError: 'NoneType' object has no attribute 'split'
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-11-05 22:06 +0000 |
| Message-ID | <l5bq5o$906$1@reader1.panix.com> |
| In reply to | #58526 |
In <l5bnpc$71v$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com> writes:
> # fetch those columns that act as lists but are stored as strings
> cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
> counterID = %s and host = %s''', (cID, host) )
> data = cur.fetchone()
> if cur.rowcount:
> # unpack data into variables
> (ref, visit, download) = data
>
> visit = visit.split()
>
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
> visit.split()
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
> AttributeError: 'NoneType' object has no attribute 'split'
It appears that in the row you're fetching from the visitors table, the
'visits' column is NULL.
Your next step is track down why.
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Nick the Gr33k <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-11-06 00:28 +0200 |
| Message-ID | <l5brem$trk$1@dont-email.me> |
| In reply to | #58529 |
Στις 6/11/2013 12:06 πμ, ο/η John Gordon έγραψε:
> In <l5bnpc$71v$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com> writes:
>
>> # fetch those columns that act as lists but are stored as strings
>> cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE
>> counterID = %s and host = %s''', (cID, host) )
>> data = cur.fetchone()
>
>> if cur.rowcount:
>> # unpack data into variables
>> (ref, visit, download) = data
>>
>> visit = visit.split()
>>
>> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
>> visit.split()
>> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
>> AttributeError: 'NoneType' object has no attribute 'split'
>
> It appears that in the row you're fetching from the visitors table, the
> 'visits' column is NULL.
>
> Your next step is track down why.
Indeed.
The code i provided only worked once before it failed and managed to
store this:
counterID,host,refs,city,userOS,browser,visits,hits,download
-------------------------------------------------------------
1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome,
0000-00-00 00:00:00, 1, ''
'visit's column is full of zeroes. Perhaps this is considered as Null
for Python?
But ref is also null, why the code didn't complain for 'ref' which is
retrieved just before 'visits'?
How, i eman what si the way to investigate this further?
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-05 22:44 +0000 |
| Message-ID | <mailman.2064.1383691475.18130.python-list@python.org> |
| In reply to | #58531 |
On 05/11/2013 22:28, Nick the Gr33k wrote:
> Στις 6/11/2013 12:06 πμ, ο/η John Gordon έγραψε:
>> In <l5bnpc$71v$1@dont-email.me> Nick the Gr33k <nikos.gr33k@gmail.com>
>> writes:
>>
>>> # fetch those columns that act as lists but are stored as
>>> strings
>>> cur.execute('''SELECT refs, visits, downloads FROM visitors
>>> WHERE
>>> counterID = %s and host = %s''', (cID, host) )
>>> data = cur.fetchone()
>>
>>> if cur.rowcount:
>>> # unpack data into variables
>>> (ref, visit, download) = data
>>>
>>> visit = visit.split()
>>>
>>> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
>>> visit.split()
>>> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
>>> AttributeError: 'NoneType' object has no attribute 'split'
>>
>> It appears that in the row you're fetching from the visitors table, the
>> 'visits' column is NULL.
>>
>> Your next step is track down why.
>
> Indeed.
>
> The code i provided only worked once before it failed and managed to
> store this:
>
>
> counterID,host,refs,city,userOS,browser,visits,hits,download
> -------------------------------------------------------------
> 1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome,
> 0000-00-00 00:00:00, 1, ''
>
> 'visit's column is full of zeroes. Perhaps this is considered as Null
> for Python?
>
> But ref is also null, why the code didn't complain for 'ref' which is
> retrieved just before 'visits'?
>
> How, i eman what si the way to investigate this further?
>
Don't worry about it. Just leave it with the crack team that I've put
together for you. We should have some answers some time tomorrow.
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web