PyWart fixed mostly, was: Re: Python Gotcha's?

2013-01-22 Thread Bryan
On Apr 14 2012, 2:47 pm I wrote:
> Miki Tebeka wrote:
> > If you have an interesting/common "Gotcha" (warts/dark corners ...) please 
> > share.
>
> Python 3(K) likes to use the same '.py' file extension as its
> incompatible predecessors, and in some/many/most *nix implementations,
> it likes to install in the same place. Python 3 is an improvement upon
> Python 2, but Python went from, "sure... Python just works," to,
> "well... that depends... which Python?"

My "gotcha" is addressed by PEP 394, "The 'python' Command on Unix-
Like Systems", and PEP 397, "A Python launcher for Windows". They
alleviate much of that pain and suffering of running both Python 2.x
and 3.x.

On *nix systems, if a Python 2 interpreter is available it should be
runnable as "python2". If Python 3 is available, it should runnable as
"python3". The modern Python shebang line is "#!/usr/bin/env python3".
[PEP 394]

On Microsoft Windows, the latest and greatest Python installation,
3.3, no longer usurps the ".py" extension for its own version-specific
interpreter. It associates ".py" with a multi-version launcher, called
"py", that makes a reasonable attempt to do the right thing py-version-
wise. The modern shebang line will help even on MS-Windows. [PEP 397]

There's room for improvement. The launcher invocation, "py", should
work on Unix. And everywhere it runs it should respect shebang lines
that name itself. The modern shebang line ought to be "#!/usr/bin/env
py -3" (but it's not yet so don't use it).

The other big idea in supporting multiple Pythons is virtual
environments. Python 3.3 has PEP 405, virtual environments in the
core. Unfortunately the aforementioned PEP 397 windows launcher, also
in Python 3.3, ignores an active virtual environment. Be warned.


-Bryan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: handling return codes from CTYPES

2013-01-22 Thread Steve Simmons

Duncan, Mike, MRAB,

Thank you.  New technology set, same forgotten lesson - RTFM (all of it!).

Thanks also for the clarification on discarding objects and Garbage 
Collection.  Looks like I'll have to turn a large chunk of my previous 
understanding of (mainframe) languages 'inside out'.


I'm just wondering how often I'll have to chant "it isn't a variable, 
it's a name bound to an object" before I can write a chunk of code 
without spending ages pondering why it isn't working.


I already like Python for its clean design but I think it'll be a while 
before I love it completely.


Steve

On 21/01/2013 11:11, Duncan Booth wrote:

Tell the function what type to return before you call it: InitScanLib
= sLib.InitScanLib InitScanLib.restype = c_short See
http://docs.python.org/2/library/ctypes.html#return-types You can also
tell it what parameter types to expect which will make calling it
simpler.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Rob Williscroft
Peter Steele wrote in
news:[email protected] in
comp.lang.python: 

> On Monday, January 21, 2013 1:10:06 AM UTC-8, Rob Williscroft wrote:
>> Peter Steele wrote in
>> 
>> news:[email protected] in
>> 
>> comp.lang.python: 
>> 
>> > I want to write a program in Python that sends a broadcast message

[snip]

>> This is part of my Wake-On-Lan script:
>> 
>> def WOL_by_mac( mac, ip = '', port = 9 ):
 
[snip]
 
> Thanks for the code sample. Does this code work if the box has no IP
> or default route assigned? I'm away from the office at the moment so I
> can't test this. 

No idea, but the sockets system must be up and running before the 
card (interface) has an IP (otherwise how would it ever get assigned) 
and I presume DHCP works in a similar manner.
 
However the "route assignemt" is irrelevent, broadcast messages never 
get routed.

Rob
-- 

-- 
http://mail.python.org/mailman/listinfo/python-list


Retrieving the full command line

2013-01-22 Thread Tim Golden
[Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
though].

I use the python -mpackage incantation to run a package which has a
__main__.py module and which uses relative imports internally.

I'm developing under cherrypy which includes a reloader for development.
The reloader attempts to rebuild the original
command line by combining sys.executable and sys.argv and then does an
execv.

There does not appear to be any way within Python of determining the
command line I used. The combination of sys.executable and sys.argv in
this case will look like: "c:\python33\python.exe app/__main__.py". But
running this precludes the use of package-relative imports.

I can pull the command line out of the Windows API to solve this
specific problem, but is there not a gap here? Even if sys.flags somehow
indicated the -m (it doesn't) that wouldn't give me the filepath which
it used.

Have I missed something?

TJG
-- 
http://mail.python.org/mailman/listinfo/python-list


Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
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'', 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
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'', 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
# ==
# 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
# 

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


Re: Using filepath method to identify an .html page

2013-01-22 Thread Lele Gaifax
Ferrous Cranus  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')


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/hashlib.html#module-hashlib
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Tue, Jan 22, 2013 at 10:53 PM, Ferrous Cranus  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread 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.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Tue, Jan 22, 2013 at 11:47 PM, Ferrous Cranus  wrote:
> 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?

The absolute path probably isn't that big. Just use it. Any form of
hashing will give you a chance of a collision.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 04:47:16 -0800, Ferrous Cranus wrote:

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


That is a very misleading name for a variable. The contents of the 
variable are not a html page, but a file name.

htmlpage = "/home/steve/my-web-page.html"  # Bad variable name.

filename = "/home/steve/my-web-page.html"  # Better variable name.



> 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.

Firstly, don't bother. What you consider "BIG", your database will 
consider trivially small. What is it, 100 characters long? 200? Unlikely 
to be 300, since I think many file systems don't support paths that long. 
But let's say it is 300 characters long.

That's likely to be 600 bytes, or a bit more than half a kilobyte. Your 
database won't even notice that.


> so to get an integer out of a string i would just have to type:
> 
> pin = int( htmlpage )

No, that doesn't work. int() does not convert arbitrary strings into 
numbers. What made you think that this could possibly work?

What do you expect int("my-web-page.html") to return? Should it return 23 
or 794 or 109432985462940911485 or 42?

> But would that be unique?

Wrong question.


Just tell your database to make the file name an indexed field, and it 
will handle giving every path a unique number for you. You can then 
forget all about that unique number, because it is completely irrelevant 
to you, and safely use the path while the database treats it in the 
fastest and most efficient fashion necessary.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: handling return codes from CTYPES

2013-01-22 Thread Chris Angelico
On Tue, Jan 22, 2013 at 7:02 PM, Steve Simmons  wrote:
> Thanks also for the clarification on discarding objects and Garbage
> Collection.  Looks like I'll have to turn a large chunk of my previous
> understanding of (mainframe) languages 'inside out'.
>
> I'm just wondering how often I'll have to chant "it isn't a variable, it's a
> name bound to an object" before I can write a chunk of code without spending
> ages pondering why it isn't working.
>
> I already like Python for its clean design but I think it'll be a while
> before I love it completely.

Yeah, it takes some getting used to. I've spent the past couple of
decades writing imperative code with strict declarations and fairly
close to the metal, and I still have one C++ module at work - but more
and more, I'm pushing to write code in a modern high-level language
like Python, where memory management isn't my problem, and failings in
the code result in easily-readable (and catchable!) exceptions. I
often fire up gdb for the C++ code at work, but usually a Python or
Pike exception traceback is enough on its own.

Learning curve, rewarded with immensely easier work. So many things
are like that; life is all about figuring out which ones are worth the
curve's effort.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 3:04:41 μ.μ. UTC+2, ο χρήστης Steven D'Aprano 
έγραψε:

> What do you expect int("my-web-page.html") to return? Should it return 23 
> or 794 or 109432985462940911485 or 42?

I expected a unique number from the given string to be produced so i could have 
a (number <=> string) relation. What does int( somestring ) is returning 
really? i don;t have IDLE to test.


> Just tell your database to make the file name an indexed field, and it 
> 
> will handle giving every path a unique number for you. You can then 
> 
> forget all about that unique number, because it is completely irrelevant 
> 
> to you, and safely use the path while the database treats it in the 
> 
> fastest and most efficient fashion necessary.

This counter.py will work on a shared hosting enviroment, so absolutes paths 
are BIG and expected like this:

/home/nikos/public_html/varsa.gr/articles/html/files/index.html

In addition to that my counter.py script maintains details in a database table 
that stores information for each and every webpage requested.

My 'visitors' database has 2 tables:

pin --- page  hits (that's to store general information for all html 
pages)

pin <-refers to-> page

pin  host  hits  useros  browser  date   (that's to store 
detailed information for all html pages)

(thousands of records to hold every page's information)


'pin' has to be a number because if i used the column 'page' instead, just 
imagine the database's capacity withholding detailed information for each and 
every .html requested by visitors!!!

So i really - really need to associate a (4-digit integer <=> htmlpage's 
absolute path)

Maybe it can be done by creating a MySQL association between the two columns, 
but i dont know how such a thing can be done(if it can).

So, that why i need to get a "unique" number out of a string. please help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 12:57 AM, Ferrous Cranus  wrote:
> Τη Τρίτη, 22 Ιανουαρίου 2013 3:04:41 μ.μ. UTC+2, ο χρήστης Steven D'Aprano 
> έγραψε:
>
>> What do you expect int("my-web-page.html") to return? Should it return 23
>> or 794 or 109432985462940911485 or 42?
>
> I expected a unique number from the given string to be produced so i could 
> have a (number <=> string) relation. What does int( somestring ) is returning 
> really? i don;t have IDLE to test.

Just run python without any args, and you'll get interactive mode. You
can try things out there.

> This counter.py will work on a shared hosting enviroment, so absolutes paths 
> are BIG and expected like this:
>
> /home/nikos/public_html/varsa.gr/articles/html/files/index.html

That's not big. Trust me, modern databases work just fine with unique
indexes like that. The most common way to organize the index is with a
binary tree, so the database has to look through log(N) entries.
That's like figuring out if the two numbers 142857 and 857142 are the
same; you don't need to look through 1,000,000 possibilities, you just
need to look through the six digits each number has.

> 'pin' has to be a number because if i used the column 'page' instead, just 
> imagine the database's capacity withholding detailed information for each and 
> every .html requested by visitors!!!

Not that bad actually. I've happily used keys easily that long, and
expected the database to ensure uniqueness without costing
performance.

> So i really - really need to associate a (4-digit integer <=> htmlpage's 
> absolute path)

Is there any chance that you'll have more than 10,000 pages? If so, a
four-digit number is *guaranteed* to have duplicates. And if you
research the Birthday Paradox, you'll find that any sort of hashing
algorithm is likely to produce collisions a lot sooner than that.

> Maybe it can be done by creating a MySQL association between the two columns, 
> but i dont know how such a thing can be done(if it can).
>
> So, that why i need to get a "unique" number out of a string. please help.

Ultimately, that unique number would end up being a foreign key into a
table of URLs and IDs. So just skip that table and use the URLs
directly - much easier. In this instance, there's no value in
normalizing.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retrieving the full command line

2013-01-22 Thread Terry Reedy

On 1/22/2013 4:24 AM, Tim Golden wrote:

[Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
though].

I use the python -mpackage incantation to run a package which has a
__main__.py module and which uses relative imports internally.

I'm developing under cherrypy which includes a reloader for development.
The reloader attempts to rebuild the original
command line by combining sys.executable and sys.argv and then does an
execv.

There does not appear to be any way within Python of determining the
command line I used. The combination of sys.executable and sys.argv in
this case will look like: "c:\python33\python.exe app/__main__.py". But
running this precludes the use of package-relative imports.


If I understand right, the reloader should be updated to translate 
'x/__main__.py' to '-m x'. Filenames of form'__x__' are reserved, in a 
sense, like similar identifiers in programs, and '__main__.py' should 
not be used for a file meant to executed directly.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 4:33:03 μ.μ. UTC+2, ο χρήστης Chris Angelico 
έγραψε:
> On Wed, Jan 23, 2013 at 12:57 AM, Ferrous Cranus  
> wrote:
> 
> > Τη Τρίτη, 22 Ιανουαρίου 2013 3:04:41 μ.μ. UTC+2, ο χρήστης Steven D'Aprano 
> > έγραψε:
> 
> >
> 
> >> What do you expect int("my-web-page.html") to return? Should it return 23
> 
> >> or 794 or 109432985462940911485 or 42?
> 
> >
> 
> > I expected a unique number from the given string to be produced so i could 
> > have a (number <=> string) relation. What does int( somestring ) is 
> > returning really? i don;t have IDLE to test.
> 
> 
> 
> Just run python without any args, and you'll get interactive mode. You
> 
> can try things out there.
> 
> 
> 
> > This counter.py will work on a shared hosting enviroment, so absolutes 
> > paths are BIG and expected like this:
> 
> >
> 
> > /home/nikos/public_html/varsa.gr/articles/html/files/index.html
> 
> 
> 
> That's not big. Trust me, modern databases work just fine with unique
> 
> indexes like that. The most common way to organize the index is with a
> 
> binary tree, so the database has to look through log(N) entries.
> 
> That's like figuring out if the two numbers 142857 and 857142 are the
> 
> same; you don't need to look through 1,000,000 possibilities, you just
> 
> need to look through the six digits each number has.
> 
> 
> 
> > 'pin' has to be a number because if i used the column 'page' instead, just 
> > imagine the database's capacity withholding detailed information for each 
> > and every .html requested by visitors!!!
> 
> 
> 
> Not that bad actually. I've happily used keys easily that long, and
> 
> expected the database to ensure uniqueness without costing
> 
> performance.
> 
> 
> 
> > So i really - really need to associate a (4-digit integer <=> htmlpage's 
> > absolute path)
> 
> 
> 
> Is there any chance that you'll have more than 10,000 pages? If so, a
> 
> four-digit number is *guaranteed* to have duplicates. And if you
> 
> research the Birthday Paradox, you'll find that any sort of hashing
> 
> algorithm is likely to produce collisions a lot sooner than that.
> 
> 
> 
> > Maybe it can be done by creating a MySQL association between the two 
> > columns, but i dont know how such a thing can be done(if it can).
> 
> >
> 
> > So, that why i need to get a "unique" number out of a string. please help.
> 
> 
> 
> Ultimately, that unique number would end up being a foreign key into a
> 
> table of URLs and IDs. So just skip that table and use the URLs
> 
> directly - much easier. In this instance, there's no value in
> 
> normalizing.
> 
> 
> 
> ChrisA

I insist, perhaps compeleld, to use a key to associate a number to a filename.
Would you help please?

I dont know this is supposed to be written. i just know i need this:

number = function_that_returns_a_number_out_of_a_string( 
absolute_path_of_a_html_file)

Would someone help me write that in python coding? We are talkign 1 line of 
code here
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Dave Angel

On 01/22/2013 09:55 AM, Ferrous Cranus wrote:

Τη Τρίτη, 22 Ιανουαρίου 2013 4:33:03 μ.μ. UTC+2, ο χρήστης Chris Angelico 
έγραψε:

On Wed, Jan 23, 2013 at 12:57 AM, Ferrous Cranus  wrote:


Τη Τρίτη, 22 Ιανουαρίου 2013 3:04:41 μ.μ. UTC+2, ο χρήστης Steven D'Aprano 
έγραψε:







What do you expect int("my-web-page.html") to return? Should it return 23



or 794 or 109432985462940911485 or 42?







I expected a unique number from the given string to be produced so i could have a 
(number <=> string) relation. What does int( somestring ) is returning really? 
i don;t have IDLE to test.




Just run python without any args, and you'll get interactive mode. You

can try things out there.




This counter.py will work on a shared hosting enviroment, so absolutes paths 
are BIG and expected like this:







/home/nikos/public_html/varsa.gr/articles/html/files/index.html




That's not big. Trust me, modern databases work just fine with unique

indexes like that. The most common way to organize the index is with a

binary tree, so the database has to look through log(N) entries.

That's like figuring out if the two numbers 142857 and 857142 are the

same; you don't need to look through 1,000,000 possibilities, you just

need to look through the six digits each number has.




'pin' has to be a number because if i used the column 'page' instead, just 
imagine the database's capacity withholding detailed information for each and 
every .html requested by visitors!!!




Not that bad actually. I've happily used keys easily that long, and

expected the database to ensure uniqueness without costing

performance.




So i really - really need to associate a (4-digit integer <=> htmlpage's 
absolute path)




Is there any chance that you'll have more than 10,000 pages? If so, a

four-digit number is *guaranteed* to have duplicates. And if you

research the Birthday Paradox, you'll find that any sort of hashing

algorithm is likely to produce collisions a lot sooner than that.




Maybe it can be done by creating a MySQL association between the two columns, 
but i dont know how such a thing can be done(if it can).







So, that why i need to get a "unique" number out of a string. please help.




Ultimately, that unique number would end up being a foreign key into a

table of URLs and IDs. So just skip that table and use the URLs

directly - much easier. In this instance, there's no value in

normalizing.



ChrisA


I insist, perhaps compeleld, to use a key to associate a number to a filename.
Would you help please?

I dont know this is supposed to be written. i just know i need this:

number = function_that_returns_a_number_out_of_a_string( 
absolute_path_of_a_html_file)

Would someone help me write that in python coding? We are talkign 1 line of 
code here



I gave you every piece of that code in my last response.  So you're not 
willing to compose the line from the clues?



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Retrieving the full command line

2013-01-22 Thread Tim Golden
On 22/01/2013 14:53, Terry Reedy wrote:
> On 1/22/2013 4:24 AM, Tim Golden wrote:
>> [Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
>> though].
>>
>> I use the python -mpackage incantation to run a package which has a
>> __main__.py module and which uses relative imports internally.
>>
>> I'm developing under cherrypy which includes a reloader for development.
>> The reloader attempts to rebuild the original
>> command line by combining sys.executable and sys.argv and then does an
>> execv.
>>
>> There does not appear to be any way within Python of determining the
>> command line I used. The combination of sys.executable and sys.argv in
>> this case will look like: "c:\python33\python.exe app/__main__.py". But
>> running this precludes the use of package-relative imports.
> 
> If I understand right, the reloader should be updated to translate
> 'x/__main__.py' to '-m x'. Filenames of form'__x__' are reserved, in a
> sense, like similar identifiers in programs, and '__main__.py' should
> not be used for a file meant to executed directly.

To be clear: it's Python itself, not the reloader, which is coming up
with __main__.py. sys.executable is "c:\python33\python.exe" and
sys.argv is ['c:\path\to\__main__.py'] for a program which has been
started by "c:\python33\python.exe -mpath\to".

Obviously, there is any number of ways around this specific issue,
including what you suggest: a canonical rewrite of "python
path\to\__main__.py" into "python -mpath\to". But it's not clear to me
that this rewrite should be the responsibility of calling code.

TJG
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 1:55 AM, Ferrous Cranus  wrote:
> I insist, perhaps compeleld, to use a key to associate a number to a filename.
> Would you help please?
>
> I dont know this is supposed to be written. i just know i need this:
>
> number = function_that_returns_a_number_out_of_a_string( 
> absolute_path_of_a_html_file)
>
> Would someone help me write that in python coding? We are talkign 1 line of 
> code here

def function_that_returns_a_number_out_of_a_string(string, cache=[]):
return cache.index(string) if string in cache else
(cache.append(string) or len(cache)-1)

That will work perfectly, as long as you don't care how long the
numbers end up, and as long as you have a single Python script doing
the work, and as long as you make sure you save and load that cache
any time you shut down the script, and so on.

It will also, and rightly, be decried as a bad idea. But hey, you did
specify that it be one line of code. For your real job, USE A DATABASE
COLUMN.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Peter Steele
I just tried running you code, and the "sendto" call fails with "Network is 
unreachable". That's what I expected, based on other tests I've done. That's 
why I was asking about how to do raw sockets, since tools like dhclient use raw 
sockets to do what they do. It can clearly be duplicated in Python, I just need 
to find some code samples on how to construct a raw packet.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 5:05:49 μ.μ. UTC+2, ο χρήστης Dave Angel έγραψε:
> On 01/22/2013 09:55 AM, Ferrous Cranus wrote:
> 
> > Τη Τρίτη, 22 Ιανουαρίου 2013 4:33:03 μ.μ. UTC+2, ο χρήστης Chris Angelico 
> > έγραψε:
> 
> >> On Wed, Jan 23, 2013 at 12:57 AM, Ferrous Cranus  
> >> wrote:
> 
> >>
> 
> >>> Τη Τρίτη, 22 Ιανουαρίου 2013 3:04:41 μ.μ. UTC+2, ο χρήστης Steven 
> >>> D'Aprano έγραψε:
> 
> >>
> 
> >>>
> 
> >>
> 
>  What do you expect int("my-web-page.html") to return? Should it return 23
> 
> >>
> 
>  or 794 or 109432985462940911485 or 42?
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> I expected a unique number from the given string to be produced so i 
> >>> could have a (number <=> string) relation. What does int( somestring ) is 
> >>> returning really? i don;t have IDLE to test.
> 
> >>
> 
> >>
> 
> >>
> 
> >> Just run python without any args, and you'll get interactive mode. You
> 
> >>
> 
> >> can try things out there.
> 
> >>
> 
> >>
> 
> >>
> 
> >>> This counter.py will work on a shared hosting enviroment, so absolutes 
> >>> paths are BIG and expected like this:
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> /home/nikos/public_html/varsa.gr/articles/html/files/index.html
> 
> >>
> 
> >>
> 
> >>
> 
> >> That's not big. Trust me, modern databases work just fine with unique
> 
> >>
> 
> >> indexes like that. The most common way to organize the index is with a
> 
> >>
> 
> >> binary tree, so the database has to look through log(N) entries.
> 
> >>
> 
> >> That's like figuring out if the two numbers 142857 and 857142 are the
> 
> >>
> 
> >> same; you don't need to look through 1,000,000 possibilities, you just
> 
> >>
> 
> >> need to look through the six digits each number has.
> 
> >>
> 
> >>
> 
> >>
> 
> >>> 'pin' has to be a number because if i used the column 'page' instead, 
> >>> just imagine the database's capacity withholding detailed information for 
> >>> each and every .html requested by visitors!!!
> 
> >>
> 
> >>
> 
> >>
> 
> >> Not that bad actually. I've happily used keys easily that long, and
> 
> >>
> 
> >> expected the database to ensure uniqueness without costing
> 
> >>
> 
> >> performance.
> 
> >>
> 
> >>
> 
> >>
> 
> >>> So i really - really need to associate a (4-digit integer <=> htmlpage's 
> >>> absolute path)
> 
> >>
> 
> >>
> 
> >>
> 
> >> Is there any chance that you'll have more than 10,000 pages? If so, a
> 
> >>
> 
> >> four-digit number is *guaranteed* to have duplicates. And if you
> 
> >>
> 
> >> research the Birthday Paradox, you'll find that any sort of hashing
> 
> >>
> 
> >> algorithm is likely to produce collisions a lot sooner than that.
> 
> >>
> 
> >>
> 
> >>
> 
> >>> Maybe it can be done by creating a MySQL association between the two 
> >>> columns, but i dont know how such a thing can be done(if it can).
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> So, that why i need to get a "unique" number out of a string. please help.
> 
> >>
> 
> >>
> 
> >>
> 
> >> Ultimately, that unique number would end up being a foreign key into a
> 
> >>
> 
> >> table of URLs and IDs. So just skip that table and use the URLs
> 
> >>
> 
> >> directly - much easier. In this instance, there's no value in
> 
> >>
> 
> >> normalizing.
> 
> >>
> 
> >>
> 
> >>
> 
> >> ChrisA
> 
> >
> 
> > I insist, perhaps compeleld, to use a key to associate a number to a 
> > filename.
> 
> > Would you help please?
> 
> >
> 
> > I dont know this is supposed to be written. i just know i need this:
> 
> >
> 
> > number = function_that_returns_a_number_out_of_a_string( 
> > absolute_path_of_a_html_file)
> 
> >
> 
> > Would someone help me write that in python coding? We are talkign 1 line of 
> > code here
> 
> >
> 
> 
> 
> I gave you every piece of that code in my last response.  So you're not 
> 
> willing to compose the line from the clues?

I cannot.
I don't even know yet if hashing needs to be used for what i need.

The only thing i know is that:

a) i only need to get a number out of string(being an absolute path)
b) That number needs to be unique, because "that" number is an indicator to the 
actual html file.

Would you help me write this in python?

Why the hell 

pin = int ( '/home/nikos/public_html/index.html' )

fails? because it has slashes in it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 2:21 AM, Ferrous Cranus  wrote:
> Why the hell
>
> pin = int ( '/home/nikos/public_html/index.html' )
>
> fails? because it has slashes in it?

What do you expect it to return? 141592653589793?

Go through the Python tutorial. Better yet, find a book that
distinguishes between technology and magic, and sets out clearly what
each one's field is.

Then go here, and read. http://www.catb.org/esr/faqs/smart-questions.html

Then go back to your project. Have another shot at things. Put your
new-found knowledge and understanding to work. You'll be far better
able to figure things out, and better able to understand what we've
all been saying.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Peter Otten
Ferrous Cranus wrote:

> I insist, perhaps compeleld, to use a key to associate a number to a
> filename. Would you help please?
> 
> I dont know this is supposed to be written. i just know i need this:
> 
> number = function_that_returns_a_number_out_of_a_string(
> absolute_path_of_a_html_file)
> 
> Would someone help me write that in python coding? We are talkign 1 line
> of code here

Since you insist:

>>> def 
>>> function_that_returns_a_number_out_of_a_string(absolute_path_of_a_html_file):
... return int(absolute_path_of_a_html_file.encode("hex"), 16)
... 
>>> function_that_returns_a_number_out_of_a_string("/foo/bar/baz")
14669632128886499728813089146L

As a bonus here is how to turn the number back into a path:

>>> x = 14669632128886499728813089146
>>> "{:x}".format(x).decode("hex")
'/foo/bar/baz'

;)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Thomas Boell
On Mon, 21 Jan 2013 06:07:08 +0100
René Klačan  wrote:

> Examples:
> 
> # else branch will be executed
> i = 0
> while i < 5:
> i += 1
> else:
> print('loop is over')
> 
> 
> # else branch will be executed
> i = 0
> while i < 5:
> i += 1
> if i == 7:
> print('i == 7')
> break
> else:
> print('loop is over')
> 
> 
> # else branch wont be executed
> i = 0
> while i < 5:
> i += 1
> if i == 3:
> print('i == 3')
> break
> else:
> print('loop is over')

Huh?! I would have expected all your examples to raise a SyntaxError or
IndentationError. Why don't they? Is 'else' not required to have a
matching 'if'?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell  wrote:
> Huh?! I would have expected all your examples to raise a SyntaxError or
> IndentationError. Why don't they? Is 'else' not required to have a
> matching 'if'?

Other things can have else, including 'for' and 'while' loops. :)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Thank you but the number needs to be a 4-digit integer only, if its to be 
stored in the database table correctly.

pin = int( htmlpage.encode("hex"), 16 )

I just tried whayt you gace me

This produces a number of: 140530319499494727...677522822126923116L

Visit http://superhost.gr to see that displayed error. I think it

Why did you use "hex" for? to encode the string to hexarithmetic? what for?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Duncan Booth
Thomas Boell  wrote:

> Huh?! I would have expected all your examples to raise a SyntaxError or
> IndentationError. Why don't they? Is 'else' not required to have a
> matching 'if'?
> 

Matching 'if' or 'for' or 'while'.
See 
http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Duncan Booth
Duncan Booth  wrote:
> Matching 'if' or 'for' or 'while'.
> 
or of course 'try'.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Thomas Boell
On Wed, 23 Jan 2013 02:42:27 +1100
Chris Angelico  wrote:

> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell  wrote:
> > Huh?! I would have expected all your examples to raise a SyntaxError or
> > IndentationError. Why don't they? Is 'else' not required to have a
> > matching 'if'?
> 
> Other things can have else, including 'for' and 'while' loops. :)

I must say, that's bound to be confusing for anyone who knows any
language other than Python (or none, even).  Syntax like that is "an
accident waiting to happen"...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 5:25:42 μ.μ. UTC+2, ο χρήστης Peter Otten έγραψε:
> Ferrous Cranus wrote:
> 
> 
> 
> > I insist, perhaps compeleld, to use a key to associate a number to a
> 
> > filename. Would you help please?
> 
> > 
> 
> > I dont know this is supposed to be written. i just know i need this:
> 
> > 
> 
> > number = function_that_returns_a_number_out_of_a_string(
> 
> > absolute_path_of_a_html_file)
> 
> > 
> 
> > Would someone help me write that in python coding? We are talkign 1 line
> 
> > of code here
> 
> 
> 
> Since you insist:
> 
> 
> 
> >>> def 
> >>> function_that_returns_a_number_out_of_a_string(absolute_path_of_a_html_file):
> 
> ... return int(absolute_path_of_a_html_file.encode("hex"), 16)
> 
> ... 
> 
> >>> function_that_returns_a_number_out_of_a_string("/foo/bar/baz")
> 
> 14669632128886499728813089146L
> 
> 
> 
> As a bonus here is how to turn the number back into a path:
> 
> 
> 
> >>> x = 14669632128886499728813089146
> 
> >>> "{:x}".format(x).decode("hex")
> 
> '/foo/bar/baz'
> 
> 
> 
> ;)

Thank you but no...no that would be unnecessary complex.

I just need a way to CONVERT a string(absolute path) to a 4-digit unique number 
with INT!!! That's all i want!! But i cannot make it work :(
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 2:48 AM, Thomas Boell  wrote:
> On Wed, 23 Jan 2013 02:42:27 +1100
> Chris Angelico  wrote:
>
>> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell  wrote:
>> > Huh?! I would have expected all your examples to raise a SyntaxError or
>> > IndentationError. Why don't they? Is 'else' not required to have a
>> > matching 'if'?
>>
>> Other things can have else, including 'for' and 'while' loops. :)
>
> I must say, that's bound to be confusing for anyone who knows any
> language other than Python (or none, even).  Syntax like that is "an
> accident waiting to happen"...

It can be confusing and does take that bit of learning, but it's SO
handy. In C code, I sometimes simulate it with a 'goto' and a comment,
but the actual keyword is much clearer.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 2:59 AM, Ferrous Cranus  wrote:
> I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> number with INT!!! That's all i want!! But i cannot make it work :(

Either you are deliberately trolling, or you have a major
comprehension problem. Please go back and read, carefully, all the
remarks you've been offered in this thread. Feel free to ask for
clarification of anything that doesn't make sense, but be sure to read
all of it. You are asking something that is fundamentally
impossible[1]. There simply are not enough numbers to go around.

ChrisA
[1] Well, impossible in decimal. If you work in base 4294967296, you
could do what you want in four "digits".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Dave Angel

On 01/22/2013 10:46 AM, Ferrous Cranus wrote:

Thank you but the number needs to be a 4-digit integer only, if its to be 
stored in the database table correctly.

pin = int( htmlpage.encode("hex"), 16 )

I just tried whayt you gace me

This produces a number of: 140530319499494727...677522822126923116L

Visit http://superhost.gr to see that displayed error. I think it

Why did you use "hex" for? to encode the string to hexarithmetic? what for?



There are plenty of people (but not me) giving you database advice, but 
you don't want it.


Apparently you do have web access, so why aren't you looking up the 
functions that don't behave the way you think they should?


This page has the built-in functions:
http://docs.python.org/2/library/functions.html

To get quickly to a particular function, follow the link from the 
function name at the top of that page.


""
int(x=0)
int(x, base=10)
Convert a number or string x to an integer, or return 0 if no arguments 
are given. If x is a number, it can be a plain integer, a long integer, 
or a floating point number. If x is floating point, the conversion 
truncates towards zero. If the argument is outside the integer range, 
the function returns a long object instead.


If x is not a number or if base is given, then x must be a string or 
Unicode object representing an integer literal in radix base. 
Optionally, the literal can be preceded by + or - (with no space in 
between) and surrounded by whitespace. A base-n literal consists of the 
digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The 
default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and 
-16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, 
as with integer literals in code. Base 0 means to interpret the string 
exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.


The integer type is described in Numeric Types — int, float, long, complex.


Are there words in there which are unclear?  A filename is a string, but 
it doesn't represent an integer literal in any base, and especially not 
in base 10.




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Ferrous Cranus
I just need a way to CONVERT a string(absolute path) to a 4-digit unique number 
with INT!!! That's all i want!! But i cannot make it work :(

And the best part is that "that" number must be able to turn back into a path.

This way i DON'T EVEN HAVE TO STORE THE ACTUAL HTML PAGE'S ABSOLUTE PATH

1. User requests a specific html page( .htaccess gives my script the absolute 
path for that .html page)
2. I turn the path into a 4-digitnumber
3. i store that number to the database. I DONT EVEN HAVE TO STORE THE PATH TO 
THE DATABASE ANYMORE!!! this is just great!
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Using filepath method to identify an .html page

2013-01-22 Thread Leonard, Arah
> Thank you but the number needs to be a 4-digit integer only, if its to be 
> stored in the database table correctly.

Okay, I think we need to throw the flag on the field at this point.  What 
you're asking for has gone into a realm where you clearly don't even appear to 
understand what you're asking for.

What is the reason for your integer being limited to only 4 digits?  Not even 
databases are limited in such a way.  So what are you doing that imposes that 
kind of a limit, and why?
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Leonard, Arah
> I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> number with INT!!! That's all i want!! But i cannot make it work :(
> 
> And the best part is that "that" number must be able to turn back into a path.
> 
> This way i DON'T EVEN HAVE TO STORE THE ACTUAL HTML PAGE'S ABSOLUTE PATH
> 
> 1. User requests a specific html page( .htaccess gives my script the absolute 
> path for that .html page) 2. I turn the path into a 4-digitnumber 3. i store 
> that number to the database. I DONT EVEN HAVE TO STORE THE PATH TO THE 
> DATABASE ANYMORE!!! this is just great!

Without involving some kind of lookup table/map service to store the paths 
(which would entirely defeat the purpose) what you are ranting about is 
technically impossible.  If you tried really really hard you *might* be able to 
convert a string that long into some kind of 4-digit integer checksum, but you 
would *never* be able to convert that back into a file path.  Nor would it be 
guaranteed to be unique.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Mark Lawrence

On 22/01/2013 16:15, Ferrous Cranus wrote:

I just need a way to CONVERT a string(absolute path) to a 4-digit unique number 
with INT!!! That's all i want!! But i cannot make it work :(

And the best part is that "that" number must be able to turn back into a path.

This way i DON'T EVEN HAVE TO STORE THE ACTUAL HTML PAGE'S ABSOLUTE PATH

1. User requests a specific html page( .htaccess gives my script the absolute 
path for that .html page)
2. I turn the path into a 4-digitnumber
3. i store that number to the database. I DONT EVEN HAVE TO STORE THE PATH TO 
THE DATABASE ANYMORE!!! this is just great!



Hi Iron Skull,

I hereby nominate you for Troll of the Millenium.

--
Cheers.

Mark Lawrence

--
http://mail.python.org/mailman/listinfo/python-list


Re: Uniquely identifying each & every html template

2013-01-22 Thread John Gordon
In <[email protected]> Ferrous Cranus 
 writes:

> > If that's the case, then I figure you have about 3 choices:
> > 1) use the file path as your key, instead of requiring a number

> No, i cannot, because it would mess things at a later time on when i for
> example:

> 1. mv name.html othername.html   (document's filename altered)
> 2. mv name.html /subfolder/name.html   (document's filepath altered)

Will the file always reside on the same device?  If so, perhaps you could
use the file inode number as the key.

(That seems fairly brittle though.  For example if the disk crashes and is
restored from a backup, the inodes could easily be different.)

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Dave Angel

On 01/22/2013 11:15 AM, Ferrous Cranus wrote:

I just need a way to CONVERT a string(absolute path) to a 4-digit unique number 
with INT!!! That's all i want!! But i cannot make it work :(

And the best part is that "that" number must be able to turn back into a path.

This way i DON'T EVEN HAVE TO STORE THE ACTUAL HTML PAGE'S ABSOLUTE PATH

1. User requests a specific html page( .htaccess gives my script the absolute 
path for that .html page)
2. I turn the path into a 4-digitnumber
3. i store that number to the database. I DONT EVEN HAVE TO STORE THE PATH TO 
THE DATABASE ANYMORE!!! this is just great!



I had prepared a detailed response, showing what your choices are with 
this new constraint.  But I can see from this post here that there's no 
point, so I've thrown it out.


Either you're trolling, or you have a very limited knowledge of 
mathematics.   This isn't a programming problem, it's a simple problem 
of information theory.


Unless you constrain your users to very restrictive filenames, what you 
ask here simply cannot be done.


Perpetual motion machine, anyone?  Or a compression algorithm which can 
be applied repeatedly to a chunk of data until it shrinks down to one 
byte?  No way to do it without cheating, and the literature is full of 
examples of people cheating.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread John Gordon
In  Ferrous Cranus 
 writes:

> I just need a way to CONVERT a string(absolute path) to a 4-digit unique
> number with INT!!! That's all i want!! But i cannot make it work :(

Given your requirements, I don't think it *can* work.  There's just no
way to do it.

How can the computer guarantee that billions of possible inputs (file paths)
map to 10,000 unique outputs (4-digit numbers)?  It's not possible.

It might be possible if you had control over the format of the input strings,
but it doesn't sound like you do.

Can you maintain a separate database which maps file paths to numbers?
If so, then this is an easy problem.  Just keep a lookup table, like so:

  filepath number
   --
  /home/files/bob/foo.html 0001
  /home/files/bob/bar.html 0002
  /home/files/steve/recipes/chocolate-cake.html0003
  /home/files/mary/payroll.html0004

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Corey LeBleu
If you don't *have* to use the actual socket library, you might want to
have a look at scapy.  It's a packet manipulation program/library. It might
make things a little easier.

http://www.secdev.org/projects/scapy/

 On Jan 22, 2013 9:17 AM, "Peter Steele"  wrote:

> I just tried running you code, and the "sendto" call fails with "Network
> is unreachable". That's what I expected, based on other tests I've done.
> That's why I was asking about how to do raw sockets, since tools like
> dhclient use raw sockets to do what they do. It can clearly be duplicated
> in Python, I just need to find some code samples on how to construct a raw
> packet.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 6:27:32 μ.μ. UTC+2, ο χρήστης Leonard, Arah έγραψε:
> > I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> > number with INT!!! That's all i want!! But i cannot make it work :(
> 
> > 
> 
> > And the best part is that "that" number must be able to turn back into a 
> > path.
> 
> > 
> 
> > This way i DON'T EVEN HAVE TO STORE THE ACTUAL HTML PAGE'S ABSOLUTE PATH
> 
> > 
> 
> > 1. User requests a specific html page( .htaccess gives my script the 
> > absolute path for that .html page) 2. I turn the path into a 4-digitnumber 
> > 3. i store that number to the database. I DONT EVEN HAVE TO STORE THE PATH 
> > TO THE DATABASE ANYMORE!!! this is just great!
> 
> 
> 
> Without involving some kind of lookup table/map service to store the paths 
> (which would entirely >defeat the purpose) what you are ranting about is 
> technically impossible.  If you tried really >really hard you *might* be able 
> to convert a string that long into some kind of 4-digit integer >checksum, 
> but you would *never* be able to convert that back into a file path.  Nor 
> would it be >guaranteed to be unique.

Now that iam thinking of it more and more, i don't have to turn the 'path' back 
to a 'number'

So, what i want is a function foo() that does this:

foo( "some long string" )  -->  1234

=
1. User requests a specific html page( .htaccess gives my script the absolute 
path for that .html page) 
2. turn the 'path' to 4-digit number and save it as 'pin'  (how?)
3. i store that number to the database. I DONT EVEN HAVE TO STORE THE HTML 
PAGE'S PATH TO THE DATABASE ANYMORE!!! this is just great! 


At some later time i want to check the weblog of that .html page


1. request the page as:  http://mydomain.gr/index.html?show=log
2. .htaccess gives my script the absolute path of the requested .html file
3. turn the 'path' to 4-digit number and save it as 'pin' (this is what i'am 
asking)
4. select all log records for that specific .html page  (based on the 'pin' 
column)


Since i have the requested 'path' which has been converted to a database stored 
4-digit number, i'am aware for which page i'am requesting detailed data from, 
so i look upon the 'pin' column in the database and thus i know which records i 
want to select.

No need, to turn the number back to a path anymore, just the path to a number, 
to identify the specific .html page

Can this be done?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread D'Arcy J.M. Cain
On Tue, 22 Jan 2013 16:27:32 +
"Leonard, Arah"  wrote:
> > I just need a way to CONVERT a string(absolute path) to a 4-digit
> > unique number with INT!!! That's all i want!! But i cannot make it
> > work :(

Why bother?  Just wish for a zillion dollars and then you never have to
program again.  At least that would be theoretically possible.

> *might* be able to convert a string that long into some kind of
> 4-digit integer checksum, but you would *never* be able to convert
> that back into a file path.  Nor would it be guaranteed to be unique.

In fact, if you have 10,001 files it is absolutely guaranteed to have at
least one duplicate entry.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
IM: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Is PyArg_ParseTuple necessary to parse arguments?

2013-01-22 Thread rahulgarg44
Or can I just loop through the argument tuple manually by using something like 
PyTuple_GET_ITEM(args,i), then putting manual code to convert the objects to 
appropriate C type?

The use case is that I am interfacing Python with another interpreter and do 
not know the type and number of arguments till runtime :)

rahul

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Leonard, Arah
> No need, to turn the number back to a path anymore, just the path to a 
> number, to identify the specific .html page
> 
> Can this be done?

Guaranteed to be unique?  Not even remotely possible.  Even with a lookup table 
approach (which defeats your purpose of not storing the path) with 4 digits 
you're looking at a maximum 1 unique file paths before your system 
duplicates numbers.  And that's the best-case scenario.  Anything else would be 
worse.

Not guaranteed to be unique?  Easy.  Just take then previously given example of 
pin = int( htmlpage.encode("hex"), 16 ) and mod it to your limit, to make:
pin = int( htmlpage.encode("hex"), 16 ) % 1
It'll give you your number, but there are no guarantees of uniqueness.  You're 
looking at more blind random luck using that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread rusi
On Jan 22, 8:59 pm, Ferrous Cranus  wrote:
> I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> number with INT!!!
> That's all i want!! But i cannot make it work :(

I just need a way to eat my soup with a screwdriver.
No I WONT use a spoon.

Im starving
HELP
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 7:24:26 μ.μ. UTC+2, ο χρήστης Leonard, Arah έγραψε:
> > No need, to turn the number back to a path anymore, just the path to a 
> > number, to identify the specific .html page
> 
> > 
> 
> > Can this be done?
> 
> 
> 
> Guaranteed to be unique?  Not even remotely possible.  Even with a lookup 
> table approach (which defeats your purpose of not storing the path) with 4 
> digits you're looking at a maximum 1 unique file paths before your system 
> duplicates numbers.  And that's the best-case scenario.  Anything else would 
> be worse.
> 
> 
> 
> Not guaranteed to be unique?  Easy.  Just take then previously given example 
> of pin = int( htmlpage.encode("hex"), 16 ) and mod it to your limit, to make:
> 
> pin = int( htmlpage.encode("hex"), 16 ) % 1
> 
> It'll give you your number, but there are no guarantees of uniqueness.  
> You're looking at more blind random luck using that.

Finally!! THANK YOU VERY MUCH!!! THIS IS WHAT I WAS LOOKING FOR!!!

NOW, if you please explain it to me from the innermost parenthesis please, 
because i do want to understand it!!!

And since i'am sure it works, and i just used it on http://superhost.gr
please view my domain and help me understand why its producing errors for me.
Your 1-line code surely works but somethings not letting my webpage load 
normally.

Please take a look
-- 
http://mail.python.org/mailman/listinfo/python-list


Understanding while...else...

2013-01-22 Thread Terry Reedy
Several people have trouble understanding Python's while-else and 
for-else constructs. It is actually quite simple if one starts with 
if-else, which few have any trouble with.


Start with, for example

if n > 0:
  n -= 1
else:
  n = None

The else clause is executed if and when the condition is false. (That 
the code is useless is not the point here.) Now use pseudo-Python label 
and goto statements to repeatedly decrement n


label: check
if n > 0:
  n -= 1
  goto: check
else:
  n = None

The else clause is executed if and when the condition is false. (I am 
aware that the above will always set n to None if it terminates normally 
and that something more is needed to do anything useful, but this is not 
the point here.) Now use a real Python while statement to do the *same 
thing*.


while n > 0:
  n -= 1
else:
  n = None

The else clause is executed if and when the condition is false. This is 
the same as with the non-problematical if statement! To see that the 
pseudo-Python is the 'correct' expansion, we can look at the 
disassembled CPython byte code.


from dis import dis
dis('if n > 0: n -= 1\nelse: n = None')
dis('while n > 0: n -= 1\nelse: n = None')

produces

  1   0 LOAD_NAME0 (n)
  3 LOAD_CONST   0 (0)
  6 COMPARE_OP   4 (>)
  9 POP_JUMP_IF_FALSE   25
 12 LOAD_NAME0 (n)
 15 LOAD_CONST   1 (1)
 18 INPLACE_SUBTRACT
 19 STORE_NAME   0 (n)
 22 JUMP_FORWARD 6 (to 31)

  2 >>   25 LOAD_CONST   2 (None)
 28 STORE_NAME   0 (n)
>>   31 LOAD_CONST   2 (None)
 34 RETURN_VALUE

  1   0 SETUP_LOOP  32 (to 35)
>>3 LOAD_NAME0 (n)
  6 LOAD_CONST   0 (0)
  9 COMPARE_OP   4 (>)
 12 POP_JUMP_IF_FALSE   28
 15 LOAD_NAME0 (n)
 18 LOAD_CONST   1 (1)
 21 INPLACE_SUBTRACT
 22 STORE_NAME   0 (n)
 25 JUMP_ABSOLUTE3
>>   28 POP_BLOCK

  2  29 LOAD_CONST   2 (None)
 32 STORE_NAME   0 (n)
>>   35 LOAD_CONST   2 (None)
 38 RETURN_VALUE

The while loop code adds SETUP_LOOP to set up the context to handle 
continue and break statements. It also add POP_BLOCK after the while 
block. I presume this disables the loop context. Most importantly for 
this discussion, JUMP_FORWARD (past the else block), which is implicit 
in if-else statements, changes to JUMP_ABSOLUTE (to the condition test), 
which is implicit in while statements and explicit in the pseudo-Python 
expansion. Everything else is the same -- in particular the 
POP_JUMP_IF_FALSE, after the condition test, which in both cases jumps 
to the else block. So in both statements, the else block is executed if 
and when the condition is false.


As for for-else statements, a for loop is basically a specialized while 
loop plus assignment. The implicit while condition is that the iterable 
has another item to process. So the else clause executes if and when 
that condition is false, when iter(iterable) is exhausted.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Mark Lawrence

On 22/01/2013 17:33, rusi wrote:

On Jan 22, 8:59 pm, Ferrous Cranus  wrote:

I just need a way to CONVERT a string(absolute path) to a 4-digit unique number 
with INT!!!
That's all i want!! But i cannot make it work :(


I just need a way to eat my soup with a screwdriver.
No I WONT use a spoon.

Im starving
HELP



Unfortunately a spoon wouldn't help in this case as the soup is being 
served in a basket.  However I still absolutely insist that a Python 
solution to this problem is found.


--
Cheers.

Mark Lawrence

--
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Peter Steele
In fact, I have used scapy in the past, but I am working in a restricted 
environment and don't have this package available. It provides tones more than 
I really need anyway, and I figured a simple raw socket send/receive can't be 
*that* hard. I may have to reverse engineer some C code, such as dhclient...

On Tuesday, January 22, 2013 8:07:12 AM UTC-8, Corey LeBleu wrote:
> If you don't *have* to use the actual socket library, you might want to have 
> a look at scapy.  It's a packet manipulation program/library. It might make 
> things a little easier.
> 
> http://www.secdev.org/projects/scapy/
> 
>  
> 
> On Jan 22, 2013 9:17 AM, "Peter Steele"  wrote:
> 
> I just tried running you code, and the "sendto" call fails with "Network is 
> unreachable". That's what I expected, based on other tests I've done. That's 
> why I was asking about how to do raw sockets, since tools like dhclient use 
> raw sockets to do what they do. It can clearly be duplicated in Python, I 
> just need to find some code samples on how to construct a raw packet.
> 
> 
> 
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 6:55:02 μ.μ. UTC+2, ο χρήστης John Gordon έγραψε:
> In  Ferrous Cranus 
>  writes:
> 
> 
> 
> > I just need a way to CONVERT a string(absolute path) to a 4-digit unique
> 
> > number with INT!!! That's all i want!! But i cannot make it work :(
> 
> 
> 
> Given your requirements, I don't think it *can* work.  There's just no
> 
> way to do it.
> 
> 
> 
> How can the computer guarantee that billions of possible inputs (file paths)
> 
> map to 10,000 unique outputs (4-digit numbers)?  It's not possible.
> 
> 
> 
> It might be possible if you had control over the format of the input strings,
> 
> but it doesn't sound like you do.
> 
> 
> 
> Can you maintain a separate database which maps file paths to numbers?
> 
> If so, then this is an easy problem.  Just keep a lookup table, like so:
> 
> 
> 
>   filepath number
> 
>    --
> 
>   /home/files/bob/foo.html 0001
> 
>   /home/files/bob/bar.html 0002
> 
>   /home/files/steve/recipes/chocolate-cake.html0003
> 
>   /home/files/mary/payroll.html0004
> 
> 
> 
> -- 
> 
> John Gordon   A is for Amy, who fell down the stairs
> 
> [email protected]  B is for Basil, assaulted by bears
> 
> -- Edward Gorey, "The Gashlycrumb Tinies"

No, because i DO NOT WANT to store LOTS OF BIGS absolute paths in the database.

And the .html files are not even close 10.000
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 6:23:16 μ.μ. UTC+2, ο χρήστης Leonard, Arah έγραψε:
> > Thank you but the number needs to be a 4-digit integer only, if its to be 
> > stored in the database table correctly.
> 
> 
> 
> Okay, I think we need to throw the flag on the field at this point.  What 
> you're asking for has gone into a realm where you clearly don't even appear 
> to understand what you're asking for.
> 
> 
> 
> What is the reason for your integer being limited to only 4 digits?  Not even 
> databases are limited in such a way.  So what are you doing that imposes that 
> kind of a limit, and why?


a) I'am a reseller, i have unlimited ftp quota, hence database space
b) I'am feeling compelled to do it this way
c) i DO NOT want to use BIG absolute paths to identify files, just small 
numbers , shich they are easier to maintain.

Your solution i know it works and i thank you very much for providing it to me!

Can you help please on the errors that http://superhost.gr gives?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Michael Torrie
On 01/22/2013 03:07 AM, Ferrous Cranus wrote:
> Now, can you pleas help me write the switch to filepath identifier?
> I'am having trouble writing it.

Unfortunately this isn't the way to go either.  Apache uses its own
config and rules to map a url to a "filepath."  There's no way for
Python to do this without interrogating Apache.  And it's not necessary
anyway.  Urls to paths are mapped in a fairly static way by Apache.
Just put your files in the right folders and generate the appropriate
urls.  It's not hard.  You're struggling because you either don't
understand how apache works, or you're trying to work against it.  I've
been deploying web sites for years and I've never had to do any of the
things you are trying to make Python do.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 7:33:00 μ.μ. UTC+2, ο χρήστης rusi έγραψε:
> On Jan 22, 8:59 pm, Ferrous Cranus  wrote:
> 
> > I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> > number with INT!!!
> 
> > That's all i want!! But i cannot make it work :(
> 
> 
> 
> I just need a way to eat my soup with a screwdriver.
> 
> No I WONT use a spoon.
> 
> 
> 
> Im starving
> 
> HELP

That was funny!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 6:11:20 μ.μ. UTC+2, ο χρήστης Chris Angelico 
έγραψε:
> On Wed, Jan 23, 2013 at 2:59 AM, Ferrous Cranus  wrote:
> 
> > I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
> > number with INT!!! That's all i want!! But i cannot make it work :(
> 
> 
> 
> Either you are deliberately trolling, or you have a major
> 
> comprehension problem. Please go back and read, carefully, all the
> 
> remarks you've been offered in this thread. Feel free to ask for
> 
> clarification of anything that doesn't make sense, but be sure to read
> 
> all of it. You are asking something that is fundamentally
> 
> impossible[1]. There simply are not enough numbers to go around.
> 
> 
> 
> ChrisA
> 
> [1] Well, impossible in decimal. If you work in base 4294967296, you
> 
> could do what you want in four "digits".

Fundamentally impossible?

Well

OK: How about this in Perl:

$ cat testMD5.pl
use strict;

foreach my $url(qw@ /index.html /about/time.html @){
hashit($url);
}

sub hashit {
   my $url=shift;
   my @ltrs=split(//,$url);
   my $hash = 0;

   foreach my $ltr(@ltrs){
$hash = ( $hash + ord($ltr)) %1;
   }
   printf "%s: %0.4d\n",$url,$hash
   
}


which yields:
$ perl testMD5.pl 
/index.html: 1066
/about/time.html: 1547
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 7:24:26 μ.μ. UTC+2, ο χρήστης Leonard, Arah έγραψε:
> > No need, to turn the number back to a path anymore, just the path to a 
> > number, to identify the specific .html page
> 
> > 
> 
> > Can this be done?
> 
> 
> 
> Guaranteed to be unique?  Not even remotely possible.  Even with a lookup 
> table approach (which defeats your purpose of not storing the path) with 4 
> digits you're looking at a maximum 1 unique file paths before your system 
> duplicates numbers.  And that's the best-case scenario.  Anything else would 
> be worse.
> 
> 
> 
> Not guaranteed to be unique?  Easy.  Just take then previously given example 
> of pin = int( htmlpage.encode("hex"), 16 ) and mod it to your limit, to make:
> 
> pin = int( htmlpage.encode("hex"), 16 ) % 1
> 
> It'll give you your number, but there are no guarantees of uniqueness.  
> You're looking at more blind random luck using that.

==
pin = int( htmlpage.encode("hex"), 16 ) % 1 
==

Can you please explain the differences to what you have posted opposed to this 
perl coding?

==
foreach my $ltr(@ltrs){
$hash = ( $hash + ord($ltr)) %1;
==

I want to understand this and see it implemented in Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Michael Torrie
I'm sorry you are getting so frustrated.  There's obviously a language
barrier here, but also your frustration is preventing you from thinking
clearly.  You need to take a step back, breath, and re-read everything
that's been written to you on this thread.  All your questions that can
be answered have been answered.  If you are being paid to develop this,
then we don't want to do your work for you since we're not the ones
being paid.  If you're doing this for a class assignment, then again we
don't want to do it for you because that would defeat the purpose of
your education.  But if you're willing to learn, then I think the others
have said on this thread will help you learn it.

You can't learn Python by developing CGI scripts and running them on the
server.  You need to try out snippets of code in an interactive way.
You've been told how to do this, and you don't need IDLE.  Although
nothing prevents you from installing IDLE on your local machine.  I hope
you have the python interpreter on your local workstation.  If not,
download it and install it.  You will need it.  Use the python standard
library reference online (or download it).  You will need it.

On 01/22/2013 08:21 AM, Ferrous Cranus wrote:
> Why the hell
>
> pin = int ( '/home/nikos/public_html/index.html' )
>
> fails? because it has slashes in it?

That line fails because the string you passed it simply cannot be parsed
into a number.  Just for simplicity's sake here, suppose we define a
number as any number of digits, 0-9, followed by a '.' or a ','
depending on locale, and some more digits, 0-9.  This is very simplistic
but it will server our purpose for this example.  So given that a number
is defined as above, we expect that we can parse the following strings:

int('123.433') == int(123.433) == 123

but '/home/nikos/public_html/index.html' contains nothing that is
recognizable as a number.  There are no 0-9 digits in it, no periods or
commas.  So rather than returning 0, which would be absolutely
incorrect, int() throws an exception because you've passed it a string
which does not contain a recognizable number.

If you really want to get a number to identify a string you'll have to
create a hash of some kind.  You were on the right track with hashlib.
Except that int() again cannot work with the hash object because nothing
in the hash object's string representation looks like a number.  If you
would follow the link you've already been given on the documentation for
hashlib you'd find that the object returned by md5 has methods you can
call to give you the hash in different forms, including a large number.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread John Gordon
In <[email protected]> Ferrous Cranus 
 writes:

> > pin int( htmlpage.encode("hex"), 16 ) % 1
> >
> > It'll give you your number, but there are no guarantees of uniqueness.
> You're looking at more blind random luck using that.

> Finally!! THANK YOU VERY MUCH!!! THIS IS WHAT I WAS LOOKING FOR!!!

No it isn't; you said you wanted a unique 4-digit number.  This method
can return the same 4-digit number for lots of different file paths.

> NOW, if you please explain it to me from the innermost parenthesis please,
> because i do want to understand it!!!

1. Transform the html path string into a (large) hexadecimal number
using the encode() function.

2. Convert the hexadecimal number into a decimal integer using the
int() function.

3. Shrink the integer into the range 0- by using the % operator.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread John Gordon
In <[email protected]> Ferrous Cranus 
 writes:

> And the .html files are not even close 10.000

You said you wanted a 4-digit number.  There are 10,000 different 4-digit
numbers.


0001
0002
...


-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread MRAB

On 2013-01-22 18:26, Ferrous Cranus wrote:

Τη Τρίτη, 22 Ιανουαρίου 2013 6:11:20 μ.μ. UTC+2, ο χρήστης Chris Angelico 
έγραψε:

On Wed, Jan 23, 2013 at 2:59 AM, Ferrous Cranus  wrote:

> I just need a way to CONVERT a string(absolute path) to a 4-digit unique 
number with INT!!! That's all i want!! But i cannot make it work :(

Either you are deliberately trolling, or you have a major
comprehension problem. Please go back and read, carefully, all the
remarks you've been offered in this thread. Feel free to ask for
clarification of anything that doesn't make sense, but be sure to read
all of it. You are asking something that is fundamentally
impossible[1]. There simply are not enough numbers to go around.

ChrisA

[1] Well, impossible in decimal. If you work in base 4294967296, you

could do what you want in four "digits".


Fundamentally impossible?


Yes.


Well

OK: How about this in Perl:

$ cat testMD5.pl
use strict;

foreach my $url(qw@ /index.html /about/time.html @){
 hashit($url);
}

sub hashit {
my $url=shift;
my @ltrs=split(//,$url);
my $hash = 0;

foreach my $ltr(@ltrs){
 $hash = ( $hash + ord($ltr)) %1;
}
printf "%s: %0.4d\n",$url,$hash

}


which yields:
$ perl testMD5.pl
/index.html: 1066
/about/time.html: 1547


That shortens the int to 4 digits.

A hash isn't guaranteed to be unique. A hash is an attempt to make an
int which is highly sensitive to a change in the data so that a small
change in the data will result in a different int. If the change is big
enough it _could_ give the same int, but the hope is that it probably
won't. (Ideally, if the hash has 4 decimal digits, you'd hope that the
chance of different data giving the same hash would be about 1 in
1.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Michael Torrie
On 01/22/2013 11:13 AM, Ferrous Cranus wrote:
> a) I'am a reseller, i have unlimited ftp quota, hence database space

Space doesn't even come into the equation.  There's virtually no
difference between a 4-digit number and a 100-character string.  Yes
there is an absolute difference in storage space, but the difference is
so miniscule that there's no point even thinking about it.  Especially
if you are dealing with less than a million database rows.

>  b) I'am feeling compelled to do it this way

Why?  Who's compelling you?  Your boss?

> c) i DO NOT want to use BIG absolute paths to identify files, just
> small numbers , shich they are easier to maintain.

No it won't be easier to maintain.  I've done my share of web
development over the years.  There's no difference between using a
string index and some form of number index.  And if you have to go over
the database by hand, having a string is infinitely easier for your
brain to comprehend than a magic number.  Now don't get me wrong.  I've
done plenty of tables linked by index numbers, but it's certainly harder
to fix the data by hand since an index number only has meaning in the
context of a query with another table.

> 
> Your solution i know it works and i thank you very much for
> providing it to me!
> 
> Can you help please on the errors that http://superhost.gr gives?

Sorry I cannot, since I don't have access to your site's source code, or
your database.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Michael Torrie
On 01/22/2013 11:26 AM, Ferrous Cranus wrote:
> which yields:
> $ perl testMD5.pl 
> /index.html: 1066
> /about/time.html: 1547

Well do it the same with in python then.  Just read the docs on the
hashlib so you know what kind of object it returns and how to call
methods on that object to return a big number that you can then do %
1 on it.  Note that your perl code is guaranteed to have collisions
in the final number generated.

If you're comfortable with perl, maybe you should use it rather than
fight a language that you are not comfortable with and not understanding.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Dave Angel

On 01/22/2013 01:26 PM, Ferrous Cranus wrote:






sub hashit {
my $url=shift;
my @ltrs=split(//,$url);
my $hash = 0;

foreach my $ltr(@ltrs){
 $hash = ( $hash + ord($ltr)) %1;
}
printf "%s: %0.4d\n",$url,$hash

}


which yields:
$ perl testMD5.pl
/index.html: 1066
/about/time.html: 1547



If you use that algorithm to get a 4 digit number, it'll look good for 
the first few files.  But if you try 100 files, you've got almost 40% 
chance of a collision, and if you try 10001, you've got a 100% chance.



So is it really okay to reuse the same integer for different files?

I tried to help you when you were using the md5 algorithm.  By using 
enough digits/characters, you can cut the likelihood of a collision 
quite small.  But 4 digits, don't be ridiculous.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Michael Torrie
On 01/22/2013 11:37 AM, Ferrous Cranus wrote:
> == pin = int(
> htmlpage.encode("hex"), 16 ) % 1 
> ==
> 
> Can you please explain the differences to what you have posted
> opposed to this perl coding?
> 
> == foreach my
> $ltr(@ltrs){ $hash = ( $hash + ord($ltr)) %1; 
> ==
> 
> I want to understand this and see it implemented in Python.

It isn't quite the thing.  The perl code is merely a checksum of the
ascii value of the characters in the file name, that is then chopped
down to a number < 1.  The Python code is taking the ascii value of
each character in the file name, converting it to a hexadecimal pair of
digits, stringing them all out into a long string, then converting that
to a number using the hexadecimal number parser. This results in a
*very* large number, 8-bits per letter in the original file name, and
then chops that down to 1.  Technically neither method is a hash and
neither will generate unique numbers.

Here's the python algorithm used on a short word:
'hello' => '68656c6c6f' (h = 0x68', e=0x65', 0x6c', 0=0x6f)
=> 0x68656c6c6f => 448378203247
mod that with 1 and you get 3247

If you would simply run the python interpreter and try these things out
you could see how and why they work or not work.  What is stopping you
from doing this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Dave Angel

On 01/22/2013 01:37 PM, Ferrous Cranus wrote:



 



==
pin = int( htmlpage.encode("hex"), 16 ) % 1
==

Can you please explain the differences to what you have posted opposed to this 
perl coding?

==
foreach my $ltr(@ltrs){
 $hash = ( $hash + ord($ltr)) %1;
==

I want to understand this and see it implemented in Python.



The perl code will produce the same hash for  "abc.html" as for 
"bca.html"  That's probably one reason Leonard didn't try to 
transliterate the buggy code.


In any case, the likelihood of a hash collision for any non-trivial 
website is substantial.  As I said elsewhere, if you hash 100 files you 
have about a 40% chance of a collision.


If you hash 220 files, the likelihood is about 90%

--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Peter Otten
Ferrous Cranus wrote:

> Τη Τρίτη, 22 Ιανουαρίου 2013 6:11:20 μ.μ. UTC+2, ο χρήστης Chris Angelico
> έγραψε:

>> all of it. You are asking something that is fundamentally
>> impossible[1]. There simply are not enough numbers to go around.

> Fundamentally impossible?
> 
> Well
> 
> OK: How about this in Perl:
> 
> $ cat testMD5.pl
> use strict;
> 
> foreach my $url(qw@ /index.html /about/time.html @){
> hashit($url);
> }
> 
> sub hashit {
>my $url=shift;
>my @ltrs=split(//,$url);
>my $hash = 0;
> 
>foreach my $ltr(@ltrs){
> $hash = ( $hash + ord($ltr)) %1;
>}
>printf "%s: %0.4d\n",$url,$hash
>
> }
> 
> 
> which yields:
> $ perl testMD5.pl
> /index.html: 1066
> /about/time.html: 1547

$ cat clashes.pl 
use strict;

foreach my $url(qw@ 
/public/fails.html
/large/cannot.html
/number/being.html
/hope/already.html
/being/really.html
/index/breath.html
/can/although.html
@){
hashit($url);
}

sub hashit {
   my $url=shift;
   my @ltrs=split(//,$url);
   my $hash = 0;

   foreach my $ltr(@ltrs){
$hash = ( $hash + ord($ltr)) %1;
   }
   printf "%s: %0.4d\n",$url,$hash
   
}
$ perl clashes.pl 
/public/fails.html: 1743
/large/cannot.html: 1743
/number/being.html: 1743
/hope/already.html: 1743
/being/really.html: 1743
/index/breath.html: 1743
/can/although.html: 1743

Hm, I must be holding it wrong...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Ferrous Cranus
Τη Τρίτη, 22 Ιανουαρίου 2013 9:02:48 μ.μ. UTC+2, ο χρήστης Michael Torrie 
έγραψε:
> On 01/22/2013 11:37 AM, Ferrous Cranus wrote:
> 
> > == pin = int(
> 
> > htmlpage.encode("hex"), 16 ) % 1 
> 
> > ==
> 
> > 
> 
> > Can you please explain the differences to what you have posted
> 
> > opposed to this perl coding?
> 
> > 
> 
> > == foreach my
> 
> > $ltr(@ltrs){ $hash = ( $hash + ord($ltr)) %1; 
> 
> > ==
> 
> > 
> 
> > I want to understand this and see it implemented in Python.
> 
> 
> 
> It isn't quite the thing.  The perl code is merely a checksum of the
> 
> ascii value of the characters in the file name, that is then chopped
> 
> down to a number < 1.  The Python code is taking the ascii value of
> 
> each character in the file name, converting it to a hexadecimal pair of
> 
> digits, stringing them all out into a long string, then converting that
> 
> to a number using the hexadecimal number parser. This results in a
> 
> *very* large number, 8-bits per letter in the original file name, and
> 
> then chops that down to 1.  Technically neither method is a hash and
> 
> neither will generate unique numbers.
> 
> 
> 
> Here's the python algorithm used on a short word:
> 
> 'hello' => '68656c6c6f' (h = 0x68', e=0x65', 0x6c', 0=0x6f)
> 
> => 0x68656c6c6f => 448378203247
> 
> mod that with 1 and you get 3247
> 
> 
> 
> If you would simply run the python interpreter and try these things out
> 
> you could see how and why they work or not work.  What is stopping you
> 
> from doing this?


May i sent you my code by mail so for you see whats wrong and 
http://superhost.gr produces error?

1. this is not a script that iam being paid for.
2, this is not a class assignemnt

I just want to use that method of gettign this to work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Oportunidade: Desenvolvedor Python/Django Sênior - RJ

2013-01-22 Thread zughumancapital
Arpex Capital Seleciona: 
Desenvolvedor Python/Django Sênior

Estamos em busca daqueles(as) que: acham que meritocracia é indispensável, 
programam desde a infância, possuem sede por aprender e programar e querem 
trabalhar muito para fazer algo especial! 

O desenvolvedor estará envolvido em um projeto Python/Django para uma das 
startups da ArpexCapital (fundo de investimentos americano com foco no mercado 
brasileiro).  

Skills necessários:
Python, HTML, MySQL

Skills desejáveis (bônus):
Framework Django, Javascript, Linux, Scrum ou XP 

Local de Trabalho: Centro/RJ
Os interessados deverão enviar o CV com pretensão salarial para 
[email protected], mencionando no assunto Desenvolvedor Python/Django 
Sênior.  


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing class from another file

2013-01-22 Thread John Gordon
In  Kevin Holleran 
 writes:

> I have a class called My_Class in a subdir called Sub_Dir.

> in My_Class.py is the following

> class My_Class_Connector:
> def __init__(self,un,pw,qs_srv="domain.com"):
> self.username = un
> self.password = pw

> Then I am trying to call from a script in the parent dir like this:

> from Sub_Dir.My_Class import *

> q_api = My_Class.My_Class_Connector(string1,string2)

Even if your import had worked, this would be wrong.  You're importing
everything from Sub_Dir.My_Class, so My_Class_Connector is in the current
namespace.  You don't need to add "My_Class." on the front (and in fact
it's an error to do so.)

> Traceback (most recent call last):
>   File "testing.py", line 1, in 
> from Sub_Dir.My_Class import *
> ImportError: No module named Sub_Dir.My_Class

Is there a file named __init__.py in Sub_Dir?  A directory must contain
that file in order to be considered a "module".  (If you don't know what
to put in the file, just leave it empty.)

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Alan Spence
On 22 Jan 2013, at 19:28, Ferrous Cranus  wrote:

> Τη Τρίτη, 22 Ιανουαρίου 2013 9:02:48 μ.μ. UTC+2, ο χρήστης Michael Torrie 
> έγραψε:
>> On 01/22/2013 11:37 AM, Ferrous Cranus wrote:
>> 
>>> == pin = int(
>> 
>>> htmlpage.encode("hex"), 16 ) % 1 
>> 
>>> ==
>> 
>>> 
>> 
>>> Can you please explain the differences to what you have posted
>> 
>>> opposed to this perl coding?
>> 
>>> 
>> 
>>> == foreach my
>> 
>>> $ltr(@ltrs){ $hash = ( $hash + ord($ltr)) %1; 
>> 
>>> ==
>> 
>>> 
>> 
>>> I want to understand this and see it implemented in Python.
>> 
>> 
>> 
>> It isn't quite the thing.  The perl code is merely a checksum of the
>> 
>> ascii value of the characters in the file name, that is then chopped
>> 
>> down to a number < 1.  The Python code is taking the ascii value of
>> 
>> each character in the file name, converting it to a hexadecimal pair of
>> 
>> digits, stringing them all out into a long string, then converting that
>> 
>> to a number using the hexadecimal number parser. This results in a
>> 
>> *very* large number, 8-bits per letter in the original file name, and
>> 
>> then chops that down to 1.  Technically neither method is a hash and
>> 
>> neither will generate unique numbers.
>> 
>> 
>> 
>> Here's the python algorithm used on a short word:
>> 
>> 'hello' => '68656c6c6f' (h = 0x68', e=0x65', 0x6c', 0=0x6f)
>> 
>> => 0x68656c6c6f => 448378203247
>> 
>> mod that with 1 and you get 3247
>> 
>> 
>> 
>> If you would simply run the python interpreter and try these things out
>> 
>> you could see how and why they work or not work.  What is stopping you
>> 
>> from doing this?
> 
> 
> May i sent you my code by mail so for you see whats wrong and 
> http://superhost.gr produces error?
> 
> 1. this is not a script that iam being paid for.
> 2, this is not a class assignemnt
> 
> I just want to use that method of gettign this to work.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

All pages, strings and objects map to:

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm

Alan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding while...else...

2013-01-22 Thread Ethan Furman

On 01/22/2013 09:44 AM, Terry Reedy wrote:

Several people have trouble understanding Python's while-else and
for-else constructs. It is actually quite simple if one starts with
if-else, which few have any trouble with.

Start with, for example

if n > 0:
   n -= 1
else:
   n = None

The else clause is executed if and when the condition is false. (That
the code is useless is not the point here.) Now use pseudo-Python label
and goto statements to repeatedly decrement n

label: check
if n > 0:
   n -= 1
   goto: check
else:
   n = None

The else clause is executed if and when the condition is false.
Now use a real Python while statement to do the *same
thing*.

while n > 0:
   n -= 1
else:
   n = None


I understand how it works (although it did take a while for it to sink 
in); my gripe, and probably why it is misunderstood so often, is that 
nine times out of ten when I /want/ to use a while-else or for-else I 
only want the true/false check /once/, at the beginning of the loop. 
/Occasionally/ I'll actually have use for the search pattern, and then I 
can use the while- or for-else construct, but that's pretty rare for me.


~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


RE: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Leonard, Arah
> The perl code will produce the same hash for  "abc.html" as for "bca.html"  
> That's probably one reason Leonard didn't try to transliterate the buggy code.
> 

Actually, to give credit where it's due, it wasn't me.  I just modified someone 
else's interesting solution in this thread and added the silly limit of 1 
to it.

> In any case, the likelihood of a hash collision for any non-trivial website 
> is substantial.
> 

Exactly.  Four digits is hardly enough range for it to be even remotely safe.  
And even then range isn't really the issue as technically it just improves your 
odds.

The results of a modulus operator are still non-unique no matter how many 
digits are there to work with ... within reason.  Statistically anyone who buys 
a ticket could potentially win the lottery no matter how bad the odds are.  ;)

And now back to the OP, I'm still confused on this four-digit limitation.  Why 
isn't the limitation at least adhering to a bytelength like byte/short/long?  
Is this database storing a string of characters instead of an actual number?  
(And if so, then why not just block out 255 characters instead of 4 to store a 
whole path?  Or at the very least treat 4 characters as 4 bytes to greatly 
increase the numeric range?)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread Dave Angel

On 01/22/2013 03:30 PM, Leonard, Arah wrote:

The perl code will produce the same hash for  "abc.html" as for "bca.html"  
That's probably one reason Leonard didn't try to transliterate the buggy code.



Actually, to give credit where it's due, it wasn't me.  I just modified someone 
else's interesting solution in this thread and added the silly limit of 1 
to it.



That's okay.  The OP doesn't seem to know anything about programming, or 
about information theory, so the fact you gave a single line that 
actually "works" must be extraordinarily valuable to him.  When he was 
trying to use the md5 module, I gave him the hints about his five 
programming errors, and was about to expand on it when i noticed his 4 
digit limitation.



In any case, the likelihood of a hash collision for any non-trivial website is 
substantial.



Exactly.  Four digits is hardly enough range for it to be even remotely safe.  
And even then range isn't really the issue as technically it just improves your 
odds.

The results of a modulus operator are still non-unique no matter how many 
digits are there to work with ... within reason.  Statistically anyone who buys 
a ticket could potentially win the lottery no matter how bad the odds are.  ;)

And now back to the OP, I'm still confused on this four-digit limitation.  Why 
isn't the limitation at least adhering to a bytelength like byte/short/long?  
Is this database storing a string of characters instead of an actual number?  
(And if so, then why not just block out 255 characters instead of 4 to store a 
whole path?  Or at the very least treat 4 characters as 4 bytes to greatly 
increase the numeric range?)



I wish I had done the internet search earlier.  This name 'ferrous 
cranus' is a pseudonym of various trolls, and anybody who'd adopt it 
isn't worth our time.


Thanks to Alan Spence for spotting that.  I'll plonk 'ferrous cranus' now.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Importing class from another file

2013-01-22 Thread Kevin Holleran
Thanks, you got me straightened out.


--
Kevin Holleran
Master of Science, Computer Information Systems
Grand Valley State University
Master of Business Administration
Western Michigan University
SANS GCFA, SANS GCFE, CCNA, ISA, MCSA, MCDST, MCP

"Do today what others won't, do tomorrow what others can't" - SEALFit

"We are what we repeatedly do. Excellence, then, is not an act, but a
habit." - Aristotle


On Tue, Jan 22, 2013 at 2:47 PM, John Gordon  wrote:

> In  Kevin Holleran <
> [email protected]> writes:
>
> > I have a class called My_Class in a subdir called Sub_Dir.
>
> > in My_Class.py is the following
>
> > class My_Class_Connector:
> > def __init__(self,un,pw,qs_srv="domain.com"):
> > self.username = un
> > self.password = pw
>
> > Then I am trying to call from a script in the parent dir like this:
>
> > from Sub_Dir.My_Class import *
>
> > q_api = My_Class.My_Class_Connector(string1,string2)
>
> Even if your import had worked, this would be wrong.  You're importing
> everything from Sub_Dir.My_Class, so My_Class_Connector is in the current
> namespace.  You don't need to add "My_Class." on the front (and in fact
> it's an error to do so.)
>
> > Traceback (most recent call last):
> >   File "testing.py", line 1, in 
> > from Sub_Dir.My_Class import *
> > ImportError: No module named Sub_Dir.My_Class
>
> Is there a file named __init__.py in Sub_Dir?  A directory must contain
> that file in order to be considered a "module".  (If you don't know what
> to put in the file, just leave it empty.)
>
> --
> John Gordon   A is for Amy, who fell down the stairs
> [email protected]  B is for Basil, assaulted by bears
> -- Edward Gorey, "The Gashlycrumb Tinies"
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to a number by using INT (no hash method)

2013-01-22 Thread John Gordon
In  Ferrous Cranus 
 writes:

> May i sent you my code by mail so for you see whats wrong and
> http://superhost.gr produces error?

I tried going to that address and got some error output.  I noticed this
in the error dump:

 186 if cursor.rowcount == 0:
 187 cursor.execute( '''INSERT INTO visitors(pin, host
   , hits, useros, browser, date) VALUES(%s, %s, %s, %s, %s)''', (pin, hos
   t, 1, useros, browser, date) )

The INSERT statement gives six column names but only five placeholders (%s)
in the VALUES clause.

Perhaps that's the problem?

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Chris Angelico
On Wed, Jan 23, 2013 at 4:57 AM, Peter Steele  wrote:
> In fact, I have used scapy in the past, but I am working in a restricted 
> environment and don't have this package available. It provides tones more 
> than I really need anyway, and I figured a simple raw socket send/receive 
> can't be *that* hard. I may have to reverse engineer some C code, such as 
> dhclient...

Yeah, I think you're working with something fairly esoteric there -
bypassing the lower tiers of support (routing etc). Chances are you
won't find any good Python examples, and C's all you'll have. Are you
reasonably familiar with C?

Point to note: Raw sockets *may* require special privileges. Some
systems require that only root employ them, for security's sake.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Peter Steele
Actually, I used to teach C, so yeah, I know it pretty well. :-)

Scapy is a possibility, I just need to add it to my environment (which doesn't 
have a C compiler). I can jury rig something though.

On Tuesday, January 22, 2013 1:19:14 PM UTC-8, Chris Angelico wrote:
> On Wed, Jan 23, 2013 at 4:57 AM, Peter Steele  wrote:
> 
> > In fact, I have used scapy in the past, but I am working in a restricted 
> > environment and don't have this package available. It provides tones more 
> > than I really need anyway, and I figured a simple raw socket send/receive 
> > can't be *that* hard. I may have to reverse engineer some C code, such as 
> > dhclient...
> 
> 
> 
> Yeah, I think you're working with something fairly esoteric there -
> 
> bypassing the lower tiers of support (routing etc). Chances are you
> 
> won't find any good Python examples, and C's all you'll have. Are you
> 
> reasonably familiar with C?
> 
> 
> 
> Point to note: Raw sockets *may* require special privileges. Some
> 
> systems require that only root employ them, for security's sake.
> 
> 
> 
> ChrisA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending a broadcast message using raw sockets

2013-01-22 Thread Rob Williscroft
Peter Steele wrote in
news:[email protected] in
comp.lang.python: 

> I just tried running you code, and the "sendto" call fails with
> "Network is unreachable". That's what I expected, based on other tests
> I've done. That's why I was asking about how to do raw sockets, since
> tools like dhclient use raw sockets to do what they do. It can clearly
> be duplicated in Python, I just need to find some code samples on how
> to construct a raw packet. 
> 

Try 
s = socket.socket( socket.AF_INET, socket.SOCK_RAW )

I tried this on windows and it needed admin privaleges to
run.

Rob.
-- 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding while...else...

2013-01-22 Thread Terry Reedy

On 1/22/2013 3:09 PM, Ethan Furman wrote:

On 01/22/2013 09:44 AM, Terry Reedy wrote:

Several people have trouble understanding Python's while-else and
for-else constructs. It is actually quite simple if one starts with
if-else, which few have any trouble with.

Start with, for example

if n > 0:
   n -= 1
else:
   n = None

The else clause is executed if and when the condition is false. (That
the code is useless is not the point here.) Now use pseudo-Python label
and goto statements to repeatedly decrement n

label: check
if n > 0:
   n -= 1
   goto: check
else:
   n = None

The else clause is executed if and when the condition is false.
Now use a real Python while statement to do the *same
thing*.

while n > 0:
   n -= 1
else:
   n = None


I understand how it works (although it did take a while for it to sink
in); my gripe, and probably why it is misunderstood so often, is that
nine times out of ten when I /want/ to use a while-else or for-else I
only want the true/false check /once/, at the beginning of the loop.


I do not understand what you are saying. There already is only one 
true/false check, at the beginning of the loop. If you only want the 
check *performed* once, you would use if-else. But I presume you know this.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pycache directories

2013-01-22 Thread monosij . forums
On Tuesday, January 22, 2013 1:01:44 AM UTC-5, Terry Reedy wrote:
> 
> > I am doing some OO python3 where I am using multiple dirs/sub-dirs.
> 
> >
> 
> > So everything works fine, however when I run code __pycache__
> 
> > directories are being created in every directory touched by the
> 
> > execution.
> 
> 
> 
> This is much better than having multiple .pyc files in every directory, 
> 
> as in Py2. You should soon learn to ignore them.
> 
> 
> 
> > Is it possible to set a configuration to be able to create these
> 
> > pycache directories in a specific location?
> 
> 
> 
> No. (I am very sure.) You can however, not have the .pyc files written, 
> 
> but that means recompile with every run. So that option is meant for 
> 
> running off a read-only medium.
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

Thanks Terry. I understand needing to adjust.
Appreciate this forum.

Monosij
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retrieving the full command line

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 15:07:18 +, Tim Golden wrote:

> On 22/01/2013 14:53, Terry Reedy wrote:
>> On 1/22/2013 4:24 AM, Tim Golden wrote:
>>> [Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
>>> though].
>>>
>>> I use the python -mpackage incantation to run a package which has a
>>> __main__.py module and which uses relative imports internally.
>>>
>>> I'm developing under cherrypy which includes a reloader for
>>> development. The reloader attempts to rebuild the original command
>>> line by combining sys.executable and sys.argv and then does an execv.
>>>
>>> There does not appear to be any way within Python of determining the
>>> command line I used. The combination of sys.executable and sys.argv in
>>> this case will look like: "c:\python33\python.exe app/__main__.py".
>>> But running this precludes the use of package-relative imports.
>> 
>> If I understand right, the reloader should be updated to translate
>> 'x/__main__.py' to '-m x'. Filenames of form'__x__' are reserved, in a
>> sense, like similar identifiers in programs, and '__main__.py' should
>> not be used for a file meant to executed directly.
> 
> To be clear: it's Python itself, not the reloader, which is coming up
> with __main__.py. sys.executable is "c:\python33\python.exe" and
> sys.argv is ['c:\path\to\__main__.py'] for a program which has been
> started by "c:\python33\python.exe -mpath\to".

I don't believe you can give direct paths to the -m flag. It uses the 
normal import mechanism to locate a module or package, so you have to 
give it a name which would be importable.

c:\python33\python.exe -m app

would work, where "app" is either a package or module:

C:\something\on\PYTHONPATH\app\__main__.py
C:\something\on\PYTHONPATH\app.py


> Obviously, there is any number of ways around this specific issue,
> including what you suggest: a canonical rewrite of "python
> path\to\__main__.py" into "python -mpath\to". But it's not clear to me
> that this rewrite should be the responsibility of calling code.


I am a bit disturbed that you cannot distinguish between:

python C:\something\on\pythonpath\app\__main__.py

python -m app


by inspecting the command line. I consider it a bug, or at least a 
misfeature, if Python transforms the command line before making it 
available in sys.argv.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Uniquely identifying each & every html template

2013-01-22 Thread Michael Torrie
On 01/21/2013 08:00 AM, Ferrous Cranus wrote:
> Τη Δευτέρα, 21 Ιανουαρίου 2013 2:47:54 μ.μ. UTC+2, ο χρήστης Joel
> Goldstick έγραψε:
>> This is trolling Ferrous.  you are a troll.  Go away
> 
> Just because you cannot answer my question that doesn't make me a
> troll you know.

It becomes trolling when you refuse to even try to understand the
answers you are given.  And you refuse to take the advise of many
knowledgeable people who have a lot more experience in web application
development than you do (I'm not including myself in this category).
You refuse to use a database in a way that it was designed to be used.
If you're unwilling to identify a file based on name, path, and
contents, then you're only solution is to use a database to associate a
particular file with an id.  When you move or rename the file you have
to update the database.  That's the only answer we can give you.  Asking
the same question over and over again on different threads is not going
to change this. This is the third thread you've started on essentially
the same question. And you're getting the same advice on each thread.
There's a reason for this!

It's apparent that you don't have a lot of background in computer
programming or algorithms.  That's okay.  No one will fault you for
this.  But if you're going to reject the solutions posed by those with
such background, then it behooves you to gain a bit of knowledge in
these areas and learn why these suggestions are going made.

I in no way intend anything I have said to be condescending.  It's just
that those who are honestly trying to help you are getting frustrated.
And possibly you will interpret this frustration as hostility or
elitism, which it really is not.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 09:33:00 -0800, rusi wrote:

> On Jan 22, 8:59 pm, Ferrous Cranus  wrote:
>> I just need a way to CONVERT a string(absolute path) to a 4-digit
>> unique number with INT!!! That's all i want!! But i cannot make it work
>> :(
> 
> I just need a way to eat my soup with a screwdriver. No I WONT use a
> spoon.
> 
> Im starving
> HELP

Very well done :-)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Michael Torrie
On 01/22/2013 04:40 PM, Steven D'Aprano wrote:
> On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:
> 
>> I'm sorry you are getting so frustrated.  There's obviously a language
>> barrier here, 
> 
> I don't think there is. The OP's posts have been written in excellent 
> English.

Well, his English is pretty good. But his quote string is Cyrillic, and
he uses phrases that are not common in everyday English, such as "I am
compelled." English is clearly his second language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Else statement executing when it shouldnt

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 16:48:35 +0100, Thomas Boell wrote:

> On Wed, 23 Jan 2013 02:42:27 +1100
> Chris Angelico  wrote:
> 
>> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell 
>> wrote:
>> > Huh?! I would have expected all your examples to raise a SyntaxError
>> > or IndentationError. Why don't they? Is 'else' not required to have a
>> > matching 'if'?
>> 
>> Other things can have else, including 'for' and 'while' loops. :)
> 
> I must say, that's bound to be confusing for anyone who knows any
> language other than Python (or none, even).  Syntax like that is "an
> accident waiting to happen"...

Oh it's even worse than that. I reckon that nearly everyone thinks that 
for...else runs the "else" clause if the for loop is empty, at least the 
first time they see it. Or for the slow of thinking like me, the first 
few dozen times.

# this is wrong, but it looks right
for x in sequence:
do_something_with(x)
else:
print "sequence is empty"


But no, that's not what it does. `for...else` is actually more like this:


# this is right
for x in sequence:
do_something_with(x)
if condition:
break
else:
print "condition was never true"


That's right. The `else` block *unconditionally* executes after the `for` 
block. The only way to skip it is to use `break`, which skips all the way 
out of the combined for...else statement.

This is a very useful feature, very badly named.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 09:33:00 -0800, rusi wrote:

> On Jan 22, 8:59 pm, Ferrous Cranus  wrote:
>> I just need a way to CONVERT a string(absolute path) to a 4-digit
>> unique number with INT!!! That's all i want!! But i cannot make it work
>> :(
> 
> I just need a way to eat my soup with a screwdriver. No I WONT use a
> spoon.
> 
> Im starving
> HELP

Very well done :-)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 10:07:21 -0800, Ferrous Cranus wrote:

> No, because i DO NOT WANT to store LOTS OF BIGS absolute paths in the
> database.

They are not big. They are tiny.

Please stop being too arrogant to listen to advice from people who have 
been programming for years or decades.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using filepath method to identify an .html page

2013-01-22 Thread Steven D'Aprano
On Tue, 22 Jan 2013 11:36:31 -0700, Michael Torrie wrote:

> I'm sorry you are getting so frustrated.  There's obviously a language
> barrier here, 

I don't think there is. The OP's posts have been written in excellent 
English.

I think we've been well and truly trolled, by somebody who even uses the 
name of a troll as his user name.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm


Thanks to Alan Spence for linking to the "Flame Warriors" web site. I 
can't believe that it took so long for anyone to realise that we were 
being trolled.

I hate to admit it, but I kind of have to admire somebody who can play 
dumb so well for so long for the lulz. Well played Ferrous Cranus, well 
played. Now please go and play your silly games elsewhere.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >