Why 'files.py' does not print the filenames into a table format?

F

Ferrous Cranus

Then how those page entries found in the database Chris?


What happens if i write it like this?

cur.execute('''SELECT ID FROM counters WHERE url = "%s"''', page )

How quoting of %s helps here?


Yes iam using a comma and not a substitute operator, so input is mysql
validates.

Please explain what is an "out-of-band argument passing method"

What your idea of those entries made it to the counters database table?

Chris? Care to explain please?
 
F

Ferrous Cranus

TB behaves for me the same way. Any line > 80 chars gets a newline.
Why this is happening? Why not post up to 256 chars in a single line?

Actually i twas a short story since i have asked this already in 2
previous threads of mine, but here it is the whole thing pasted in
pastebin. Its not so biug and with your talent you could understand it
in aprox. 5 mins.

http://pastebin.com/XgWKuXUC

Someone saw something that will help explain why the utf-8 converted
filenames refuse to appear?
 
A

alex23

Congratulation. You have just entered an extremely exclusive club. See
you in a month.

*plonk*

So yours are the only pissy one-liner responses that shouldn't be
taken off-list?
 
M

Mark Lawrence

So yours are the only pissy one-liner responses that shouldn't be
taken off-list?

I suggest caution, Big Brother is watching *YOU* :)

--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.

Mark Lawrence
 
Î

Îίκος

Look, Nick,

A lot of people are frustrated by you. You should understand that. If
you cannot, you need to step back and consider, or you really are a
troll.

Now, obviously it's not going to get you any help to have half of the
forum angry at you. People have stopped helping, at least in large.
This is fine; people here are volunteers. But you want help.

So, Nick, listen. You need to learn how to ask *smart* questions. If
you do, I *guarantee* that people will respect you a lot more. I'll be
willing to give a bit of time to explain what I mean.

1) What is your problem. Not "I want to know why it doesn't print
anything." Here's an example, for some random idea:


2) What have you tried? What debugging have you done? For someone of
your skill level, it's also important to tell us what you think your
code is doing. Example:


3) If possible, give us an example we can run.


Do you see the difference?


Sure. Here I go:

What's the question?


I DID, I FINALLY DID IT JUST NOW!!

HERE ARE THE MODIFICATIONS THAT MADE IT HAPPEN!


==========================================================================================================
# Convert wrongly encoded filenames to utf-8
==========================================================================================================

path = b'/home/nikos/public_html/data/apps/'
filenames = os.listdir( path )

utf8_filenames = []

for filename in filenames:
# Compute 'path/to/filename'
filename_bytes = path + filename
encoding = guess_encoding( filename_bytes )

if encoding == 'utf-8':
# File name is valid UTF-8, so we can skip to the next file.
utf8_filenames.append( filename_bytes )
continue
elif encoding is None:
# No idea what the encoding is. Hit it with a hammer until it stops
moving.
filename = filename_bytes.decode( 'utf-8', 'xmlcharrefreplace' )
else:
filename = filename_bytes.decode( encoding )

# Rename the file to something which ought to be UTF-8 clean.
newname_bytes = filename.encode('utf-8')
os.rename( filename_bytes, newname_bytes )
utf8_filenames.append( newname_bytes )

# Once we get here, the file ought to be UTF-8 clean and the Unicode
name ought to exist:
assert os.path.exists( newname_bytes.decode('utf-8') )
================================

i SMASHED MY HEAD INTO THE WALL, BUT I MADE IT!!!!
FINALLY AFTER > 15 DAYS!!

FEEL FREE TO CONGRATULATE ME!
 
Î

Îίκος

Look, Nick,

A lot of people are frustrated by you. You should understand that. If
you cannot, you need to step back and consider, or you really are a
troll.

Now, obviously it's not going to get you any help to have half of the
forum angry at you. People have stopped helping, at least in large.
This is fine; people here are volunteers. But you want help.

So, Nick, listen. You need to learn how to ask *smart* questions. If
you do, I *guarantee* that people will respect you a lot more. I'll be
willing to give a bit of time to explain what I mean.

1) What is your problem. Not "I want to know why it doesn't print
anything." Here's an example, for some random idea:


2) What have you tried? What debugging have you done? For someone of
your skill level, it's also important to tell us what you think your
code is doing. Example:


3) If possible, give us an example we can run.


Do you see the difference?


Sure. Here I go:

What's the question?


I DID, I FINALLY DID IT JUST NOW!!

HERE ARE THE MODIFICATIONS THAT MADE IT HAPPEN!


==========================================================================================================

# Convert wrongly encoded filenames to utf-8
==========================================================================================================


path = b'/home/nikos/public_html/data/apps/'
filenames = os.listdir( path )

utf8_filenames = []

for filename in filenames:
# Compute 'path/to/filename'
filename_bytes = path + filename
encoding = guess_encoding( filename_bytes )

if encoding == 'utf-8':
# File name is valid UTF-8, so we can skip to the next file.
utf8_filenames.append( filename_bytes )
continue
elif encoding is None:
# No idea what the encoding is. Hit it with a hammer until it
stops moving.
filename = filename_bytes.decode( 'utf-8', 'xmlcharrefreplace' )
else:
filename = filename_bytes.decode( encoding )

# Rename the file to something which ought to be UTF-8 clean.
newname_bytes = filename.encode('utf-8')
os.rename( filename_bytes, newname_bytes )
utf8_filenames.append( newname_bytes )

# Once we get here, the file ought to be UTF-8 clean and the
Unicode name ought to exist:
assert os.path.exists( newname_bytes.decode('utf-8') )
================================

i SMASHED MY HEAD INTO THE WALL, BUT I MADE IT!!!!
FINALLY AFTER > 15 DAYS!!

FEEL FREE TO CONGRATULATE ME!

oups!

everything work as expected but not the part when the counter of a
filename gets increased when the file have been requested.

I don't see how since:

if filename:
#update file counter
cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit =
%s WHERE url = %s''', (host, lastvisit, filename) )
 
S

Steven D'Aprano

everything work as expected but not the part when the counter of a
filename gets increased when the file have been requested.

I don't see how since:

if filename:
#update file counter
cur.execute('''UPDATE files SET hits = hits + 1, host = %s,
lastvisit = %s WHERE url = %s''', (host, lastvisit, filename) )


Have you read these links yet?

http://sscce.org/‎

http://www.catb.org/esr/faqs/smart-questions.html‎


They will teach you how to successfully ask for help.
 
S

Simpleton

There isn'tmuch to say ehre. You already know the code that im using
inside files.pu and the question is that this execute never gets to execute.

#
=================================================================================================================
# Make sure that ALL database records are filenames in existance
#
=================================================================================================================
filenames = []

# Switch filenames from (utf8 bytestrings => unicode strings) and trim
them from their paths
for utf8_filename in utf8_filenames:
filenames.append( utf8_filename.decode('utf-8').replace(
'/home/nikos/public_html/data/apps/', '' ) )

# Check the presence of a database file against the dir files and delete
record if it doesn't exist
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()

# Delete spurious database records
for rec in data:
if rec not in filenames:
cur.execute('''DELETE FROM files WHERE url = %s''', rec )

# Load'em
for filename in filenames:
try:
# Check the presence of current filename against it's database presence
cur.execute('''SELECT url FROM files WHERE url = %s''', filename )
data = cur.fetchone()

if not data:
# First time for file; primary key is automatic, hit is defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit) VALUES (%s,
%s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )


#
=================================================================================================================
# Display ALL files, each with its own download button
#
=================================================================================================================
print('''<body background='/data/images/star.jpg'>
<center><img src='/data/images/download.gif'><br><br>
<table border=5 cellpadding=5 bgcolor=green>
''')

try:
cur.execute( '''SELECT * FROM files ORDER BY lastvisit DESC''' )
data = cur.fetchall()

for row in data:
(filename, hits, host, lastvisit) = row
lastvisit = lastvisit.strftime('%A %e %b, %H:%M')

print('''
<form method="get" action="/cgi-bin/files.py">
<tr>
<td> <center> <input type="submit" name="filename" value="%s"> </td>
<td> <center> <font color=yellow size=5> %s </td>
<td> <center> <font color=orange size=4> %s </td>
<td> <center> <font color=silver size=4> %s </td>
</tr>
</form>
''' % (filename, hits, host, lastvisit) )
print( '''</table><br><br>''' )
except pymysql.ProgrammingError as e:
print( repr(e) )

sys.exit(0)

After a spcific file gets selected then files.py is reloading grabbign
the filename as a variable form and:

#
=================================================================================================================
# If user downloaded a file, thank the user !!!
#
=================================================================================================================
if filename:
#update filename's counter if cookie does not exist
cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit =
%s WHERE url = %s''', (host, lastvisit, filename) )

but the execute never happesn.
i ahve tested it

if data:
print soemthing

but data is always empty.
 
S

Simpleton

# Load'em
for filename in filenames:
try:
# Check the presence of current filename against it's database
presence
cur.execute('''SELECT url FROM files WHERE url = %s''', filename )
data = cur.fetchone()

if not data:
# First time for file; primary key is automatic, hit is
defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit)
VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )


Also i just alternated the above code to:

# Load'em
for filename in filenames:
try:
# try to insert the file into the database
cur.execute('''INSERT INTO files (url, host, lastvisit) VALUES (%s,
%s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
# Insertion failed, file already into database, skip this, go to next
filename
pass

Isn't more compact and straightforward this way?
but i have to set the url's type into unique type for the abpve to work?
 
S

Simpleton

There isn'tmuch to say ehre. You already know the code that im using
inside files.pu and the question is that this execute never gets to
execute.

#
=================================================================================================================

# Make sure that ALL database records are filenames in existance
#
=================================================================================================================

filenames = []

# Switch filenames from (utf8 bytestrings => unicode strings) and trim
them from their paths
for utf8_filename in utf8_filenames:
filenames.append( utf8_filename.decode('utf-8').replace(
'/home/nikos/public_html/data/apps/', '' ) )

# Check the presence of a database file against the dir files and delete
record if it doesn't exist
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()

# Delete spurious database records
for rec in data:
if rec not in filenames:
cur.execute('''DELETE FROM files WHERE url = %s''', rec )

# Load'em
for filename in filenames:
try:
# Check the presence of current filename against it's database
presence
cur.execute('''SELECT url FROM files WHERE url = %s''', filename )
data = cur.fetchone()

if not data:
# First time for file; primary key is automatic, hit is
defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit)
VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )


#
=================================================================================================================

# Display ALL files, each with its own download button
#
=================================================================================================================

print('''<body background='/data/images/star.jpg'>
<center><img src='/data/images/download.gif'><br><br>
<table border=5 cellpadding=5 bgcolor=green>
''')

try:
cur.execute( '''SELECT * FROM files ORDER BY lastvisit DESC''' )
data = cur.fetchall()

for row in data:
(filename, hits, host, lastvisit) = row
lastvisit = lastvisit.strftime('%A %e %b, %H:%M')

print('''
<form method="get" action="/cgi-bin/files.py">
<tr>
<td> <center> <input type="submit" name="filename"
value="%s"> </td>
<td> <center> <font color=yellow size=5> %s </td>
<td> <center> <font color=orange size=4> %s </td>
<td> <center> <font color=silver size=4> %s </td>
</tr>
</form>
''' % (filename, hits, host, lastvisit) )
print( '''</table><br><br>''' )
except pymysql.ProgrammingError as e:
print( repr(e) )

sys.exit(0)

After a spcific file gets selected then files.py is reloading grabbign
the filename as a variable form and:

#
=================================================================================================================

# If user downloaded a file, thank the user !!!
#
=================================================================================================================

if filename:
#update filename's counter if cookie does not exist
cur.execute('''UPDATE files SET hits = hits + 1, host = %s,
lastvisit = %s WHERE url = %s''', (host, lastvisit, filename) )

but the execute never happesn.
i ahve tested it

if data:
print soemthing

but data is always empty.

So any ideas why the update statements never gets executed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top