Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using filepath method to identify an .html page

Reply
Thread Tools

Using filepath method to identify an .html page

 
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
Hello, i decided to switch from embedding string into .html to actually grab the filepath in order to identify it:

# ================================================== ================================================== =============
# open current html template and get the page ID number
# ===============================

f = open( page )

# read first line of the file
firstline = f.readline()

# find the ID of the file and store it
pin = re.match( r'<!-- (\d+) -->', firstline ).group(1)
=================================

This is what i used to have.

Now, can you pleas help me write the switch to filepath identifier?
I'am having trouble writing it.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-22-2013
On Tue, 22 Jan 2013 02:07:54 -0800, Ferrous Cranus wrote:

> Hello, i decided to switch from embedding string into .html to actually
> grab the filepath in order to identify it:


What do you think "the filepath" means, and how do you think you would
grab it?

I can only guess you mean the full path to the file, like:

/home/steve/mypage.html

C:\My Documents\mypage.html


Is that what you mean?


> # open current html template and get the page ID number
> f = open( page )
> # read first line of the file
> firstline = f.readline()
> # find the ID of the file and store it
> pin = re.match( r'<!-- (\d+) -->', firstline ).group(1)
>
> This is what i used to have.
>
> Now, can you pleas help me write the switch to filepath identifier? I'am
> having trouble writing it.


I don't understand the question.


--
Steven
 
Reply With Quote
 
 
 
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
# ==================================================
# produce a hash based on html page's filepath and convert it to an integet, that will be uses to identify the page itself.
# ==================================================

pin = int( hashlib.md5( htmlpage ) )


I just tried that but it produced an error.
What am i doing wrong?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
# ================================================== ================================================== ================================
# produce a hash string based on html page's filepath and convert it to an integer, that will then be used to identify the page itself
# ================================================== ================================================== ================================

pin = int( hashlib.md5( htmlpage ) )

This fails. why?

htmlpage = a string respresenting the absolute path of the requested .html file
hashlib.md5( htmlpage ) = conversion of the above string to a hashed string
int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number

Why this fails?
 
Reply With Quote
 
Lele Gaifax
Guest
Posts: n/a
 
      01-22-2013
Ferrous Cranus <(E-Mail Removed)> writes:

> pin = int( hashlib.md5( htmlpage ) )
>
> This fails. why?
>
> htmlpage = a string respresenting the absolute path of the requested .html file
> hashlib.md5( htmlpage ) = conversion of the above string to a hashed string


No, that statement does not "convert" a string into another, but rather
returns a "md5 HASH object":

>>> import hashlib
>>> hashlib.md5('foo')

<md5 HASH object @ 0xb76dbcf0>

Consulting the hashlib documentation[1], you could learn about that
object's methods.

> int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number
>
> Why this fails?


Because in general you can't "convert" an arbitrary object instance (in
your case an hashlib.HASH instance) to an integer value. Why do you need
an integer? Isn't hexdigest() what you want?

>>> print _.hexdigest()

acbd18db4cc2f85cedef654fccc4a4d8

Do yourself a favor and learn using the interpreter to test your
snippets line by line, most problems will find an easy answer

ciao, lele.

[1] http://docs.python.org/2.7/library/h...module-hashlib
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
http://www.velocityreviews.com/forums/(E-Mail Removed) | -- Fortunato Depero, 1929.

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-22-2013
On Tue, Jan 22, 2013 at 10:53 PM, Ferrous Cranus <(E-Mail Removed)> wrote:
> # ==================================================
> # produce a hash based on html page's filepath and convert it to an integet, that will be uses to identify the page itself.
> # ==================================================
>
> pin = int( hashlib.md5( htmlpage ) )
>
>
> I just tried that but it produced an error.
> What am i doing wrong?


First and foremost, here's what you're doing wrong: You're saying "it
produced an error". Python is one of those extremely helpful languages
that tells you, to the best of its ability, exactly WHAT went wrong,
WHERE it went wrong, and - often - WHY it failed. For comparison, I've
just tonight been trying to fix up a legacy accounting app that was
written in Visual BASIC back when that wouldn't get scorn heaped on
you from the whole world. When we fire up one particular module, it
bombs with a little message box saying "File not found". That's all.
Just one little message, and the application terminates (uncleanly, at
that). What file? How was it trying to open it? I do know that it
isn't one of its BTrieve data files, because when one of THEM isn't
found, the crash looks different (but it's still a crash). My current
guess is that it's probably a Windows DLL file or something, but it's
really not easy to tell...

ChrisA
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      01-22-2013
On 01/22/2013 07:02 AM, Ferrous Cranus wrote:
> # ================================================== ================================================== ================================
> # produce a hash string based on html page's filepath and convert it to an integer, that will then be used to identify the page itself
> # ================================================== ================================================== ================================
>
> pin = int( hashlib.md5( htmlpage ) )
>
> This fails. why?
>
> htmlpage = a string respresenting the absolute path of the requested .html file
> hashlib.md5( htmlpage ) = conversion of the above string to a hashed string
> int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number
>
> Why this fails?
>


Is your copy/paste broken? It could be useful to actually show in what
way it "fails."

The md5 method produces a "HASH object", not a string. So int() cannot
process that.

To produce a digest string from the hash object, you want to call
hexdigest() method. The result of that is a hex literal string. So you
cannot just call int() on it, since that defaults to decimal.

To convert a hex string to an int, you need the extra parameter of int:

int(mystring, 16)

Now, see if you can piece it together.


--
DaveA
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
Τη Τρίτη, 22 Ιανουαρίου 2013 2:29:21 μ.μ. UTC+2, οχρήστης Dave Angel *γραψε:
> On 01/22/2013 07:02 AM, Ferrous Cranus wrote:
>
> > # ================================================== ================================================== ================================

>
> > # produce a hash string based on html page's filepath and convert it toan integer, that will then be used to identify the page itself

>
> > # ================================================== ================================================== ================================

>
> >

>
> > pin = int( hashlib.md5( htmlpage ) )

>
> >

>
> > This fails. why?

>
> >

>
> > htmlpage = a string respresenting the absolute path of the requested ..html file

>
> > hashlib.md5( htmlpage ) = conversion of the above string to a hashed string

>
> > int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number

>
> >

>
> > Why this fails?

>
> >

>
>
>
> Is your copy/paste broken? It could be useful to actually show in what
>
> way it "fails."
>
>
>
> The md5 method produces a "HASH object", not a string. So int() cannot
>
> process that.
>
>
>
> To produce a digest string from the hash object, you want to call
>
> hexdigest() method. The result of that is a hex literal string. So you
>
> cannot just call int() on it, since that defaults to decimal.
>
>
>
> To convert a hex string to an int, you need the extra parameter of int:
>
>
>
> int(mystring, 16)
>
>
>
> Now, see if you can piece it together.
>



htmlpage = a string respresenting the absolute path of the requested .html file


What i want to do, is to associate a number to an html page's absolute pathfor to be able to use that number for my database relations instead of theBIG absolute path string.

so to get an integer out of a string i would just have to type:

pin = int( htmlpage )

But would that be unique?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
Τη Τρίτη, 22 Ιανουαρίου 2013 2:29:21 μ.μ. UTC+2, οχρήστης Dave Angel *γραψε:
> On 01/22/2013 07:02 AM, Ferrous Cranus wrote:
>
> > # ================================================== ================================================== ================================

>
> > # produce a hash string based on html page's filepath and convert it toan integer, that will then be used to identify the page itself

>
> > # ================================================== ================================================== ================================

>
> >

>
> > pin = int( hashlib.md5( htmlpage ) )

>
> >

>
> > This fails. why?

>
> >

>
> > htmlpage = a string respresenting the absolute path of the requested ..html file

>
> > hashlib.md5( htmlpage ) = conversion of the above string to a hashed string

>
> > int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number

>
> >

>
> > Why this fails?

>
> >

>
>
>
> Is your copy/paste broken? It could be useful to actually show in what
>
> way it "fails."
>
>
>
> The md5 method produces a "HASH object", not a string. So int() cannot
>
> process that.
>
>
>
> To produce a digest string from the hash object, you want to call
>
> hexdigest() method. The result of that is a hex literal string. So you
>
> cannot just call int() on it, since that defaults to decimal.
>
>
>
> To convert a hex string to an int, you need the extra parameter of int:
>
>
>
> int(mystring, 16)
>
>
>
> Now, see if you can piece it together.
>



htmlpage = a string respresenting the absolute path of the requested .html file


What i want to do, is to associate a number to an html page's absolute pathfor to be able to use that number for my database relations instead of theBIG absolute path string.

so to get an integer out of a string i would just have to type:

pin = int( htmlpage )

But would that be unique?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      01-22-2013
Τη Τρίτη, 22 Ιανουαρίου 2013 2:47:16 μ.μ. UTC+2, οχρήστης Ferrous Cranus *γραψε:
> Τη Τρίτη, 22 Ιανουαρίου 2013 2:29:21 μ.μ. UTC+2, ο χρήστης Dave Angel *γραψε:
>
> > On 01/22/2013 07:02 AM, Ferrous Cranus wrote:

>
> >

>
> > > # ================================================== ================================================== ================================

>
> >

>
> > > # produce a hash string based on html page's filepath and convert it to an integer, that will then be used to identify the page itself

>
> >

>
> > > # ================================================== ================================================== ================================

>
> >

>
> > >

>
> >

>
> > > pin = int( hashlib.md5( htmlpage ) )

>
> >

>
> > >

>
> >

>
> > > This fails. why?

>
> >

>
> > >

>
> >

>
> > > htmlpage = a string respresenting the absolute path of the requested .html file

>
> >

>
> > > hashlib.md5( htmlpage ) = conversion of the above string to a hashed string

>
> >

>
> > > int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number

>
> >

>
> > >

>
> >

>
> > > Why this fails?

>
> >

>
> > >

>
> >

>
> >

>
> >

>
> > Is your copy/paste broken? It could be useful to actually show in what

>
> >

>
> > way it "fails."

>
> >

>
> >

>
> >

>
> > The md5 method produces a "HASH object", not a string. So int() cannot

>
> >

>
> > process that.

>
> >

>
> >

>
> >

>
> > To produce a digest string from the hash object, you want to call

>
> >

>
> > hexdigest() method. The result of that is a hex literal string. So you

>
> >

>
> > cannot just call int() on it, since that defaults to decimal.

>
> >

>
> >

>
> >

>
> > To convert a hex string to an int, you need the extra parameter of int:

>
> >

>
> >

>
> >

>
> > int(mystring, 16)

>
> >

>
> >

>
> >

>
> > Now, see if you can piece it together.

>
> >

>
>
>
>
>
> htmlpage = a string respresenting the absolute path of the requested .html file
>
>
>
>
>
> What i want to do, is to associate a number to an html page's absolute path for to be able to use that number for my database relations instead of the BIG absolute path string.
>
>
>
> so to get an integer out of a string i would just have to type:
>
>
>
> pin = int( htmlpage )
>
>
>
> But would that be unique?


Another error even without hasing anyhting http://superhost.gr to view it please
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
filepath in identify for paperclip cygwin Dan Quach Ruby 3 01-06-2011 08:14 AM
How to identify whether a function is module scoped function orstatic method of a class by using its fully qualified name srinivasan srinivas Python 0 07-02-2008 10:32 AM
how to request full filepath to a default page? darrel ASP .Net 0 06-24-2005 04:07 PM
current filepath? Lauchlan M ASP .Net 1 08-18-2003 12:04 AM
How to identify the method that has called another method ? Mars Python 2 07-19-2003 04:34 PM



Advertisments