Re: Database engine bindings for Python (was: Database statements via python but database left intact)

2013-10-05 Thread Dan Sommers
On Sun, 06 Oct 2013 12:19:13 +1100, Chris Angelico wrote:

> On Sun, Oct 6, 2013 at 12:05 PM, Ben Finney wrote:

[ ... ]

>> With a separately-installed, far more complex database engine like
>> MySQL or PostgreSQL, the Python bindings will only work if they are
>> compiled against the correct client library.

[ ... ]

> The alternative is a pure-Python implementation of the PostgreSQL wire
> protocol ... I don't know if one exists already or not ...

The PostgreSQL Python wiki [0] says that three such implementations do
exist.  Some time ago, before psycopg worked with Python3, I tried
pg8000, and I could access my local databases, but I never got further
than that.

- Dan

[0] http://wiki.postgresql.org/wiki/Python
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to read list from file

2013-10-05 Thread Terry Reedy

On 10/5/2013 9:08 PM, Harvey Greenberg wrote:

I am looping as for L in file.readlines(), where file is csv.


I believe 'for L in file:' does the same, more efficiently, even in 2.7.

--
Terry Jan Reedy

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 6/10/2013 2:36 πμ, ο/η Denis McMahon έγραψε:

On Sat, 05 Oct 2013 16:38:14 +0300, Νίκος Αλεξόπουλος wrote:


[my cookie code is fucked]


Hi Nick

I had never used python for www before yesterday. I had never used python
before about 6 months ago.

In the last 24 hours I have installed mod-wsgi on my apache web server,
and written test code that enables me to see the result of all
environment data, parse the get string, parse post data, set and parse
cookies, and save session data to a file specific to the cookie data.

There may be better ways to do some of it, but it works.

Here is a link to the text of my python file:

http://www.sined.co.uk/tmp/index.py.txt

Once you have read through it (there are comments) and understood how it
handles cookies and session data, you will realise that you can add and
modify any session data just by adding relevant items to and reading them
from the session data dictionary. The session data stays on the server,
the cookie simply identifies the session (and the session data file), and
hopefully is unique to the user.

Don't ask me any questions about why I did something the way I did it. I
did it that way because it works. If anyone knows a different way that
also works and maybe works better, feel free to discuss it. But Nick,
don't you dare suggest any change that doesn't work because you think it
looks better.

If you don't understand a module function that I've used, read the python
documentation for it.

If you think you have a question that is  not covered by the above
statements, feel free to ask me, here, why I wrote a given line number or
group of line numbers the way I did. However, see the notes above - the
answer will probably be "because it works that way".

If you quote the whole file or even large chunks of it here, I am
finished with trying to help you, ever!


Thank you Denis, i didn't knew about sessions up until i saw you post.
Your code is very advanced for me to read but i will try to "decode it"
I though that if we want to read something from our visitor, something 
we want, we have to save it to his browser a cookie, that we later 
retrieve within our python script's code.


What "sessions" do more compared to just using cookies?

i use 'cgi', but i noticed you used 'mod-wsgi'.
I want to ask you why you chose the latter? Is the latter better than 
the former?


Is it faster? Does it bahave the same way?
I my code works works with cgi, which it does, will the same code works 
with mod-wcgi?



--
What is now proved was at first only imagined! & WebHost

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Steven D'Aprano
On Sat, 05 Oct 2013 20:17:32 -0700, galeomaga wrote:


> if __name__ == '__main__':
>   try:
>   thread.start_new_thread( readfile, ("Thread-1", ) )
>   except:
>   print "Error: unable to start thread"


Why not? If you can't start a thread, you have a problem with your code. 
How do you expect to debug this problem?


"I find it amusing when novice programmers believe their main job is 
preventing programs from crashing. More experienced programmers realize 
that correct code is great, code that crashes could use improvement, but 
incorrect code that doesn’t crash is a horrible nightmare."
-- Chris Smith

http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/


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


Variable arguments (*args, **kwargs): seeking elegance

2013-10-05 Thread John Ladasky
Hi folks,

I'm trying to make some of Python class definitions behave like the ones I find 
in professional packages, such as Matplotlib.  A Matplotlib class can often 
have a very large number of arguments -- some of which may be optional, some of 
which will assume default values if the user does not override them, etc.

I have working code which does this kind of thing.  I define required arguments 
and their default values as a class attribute, in an OrderedDict, so that I can 
match up defaults, in order, with *args.  I'm using set.issuperset() to see if 
an argument passed in **kwargs conflicts with one which was passed in *args.  I 
use  set.isdisjoint() to look for arguments in **kwargs which are not expected 
by the class definition, raising an error if such arguments are found.

Even though my code works, I'm finding it to be a bit clunky.  And now, I'm 
writing a new class which has subclasses, and so actually keeps the "extra" 
kwargs instead of raising an error... This is causing me to re-evaluate my 
original code.

It also leads me to ask: is there a CLEAN and BROADLY-APPLICABLE way for 
handling the *args/**kwargs/default values shuffle that I can study?  Or is 
this sort of thing too idiosyncratic for there to be a general method?

Thanks for any pointers!

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Chris Angelico
On Sun, Oct 6, 2013 at 2:17 PM,   wrote:
> After tried many times, updated text file is not shown, it only print text at 
> the first time.

The implementation of tail has a lot of little oddities to deal with
edge cases. Why not simply use it?

A while ago, I wanted to make a system that would tail a bunch of logs
on a bunch of computers, and display it all to me in a single unified
view. Rather than write something that opened a whole lot of files and
monitored them, I simply forked a 'tail' process for each file and
reacted to its stdout. It was way WAY easier than dealing with
everything that could possibly happen (log rotation, etc, etc, etc) -
not that it'd be impossible to deal with, but it's a waste of time
reinventing this particular wheel. Build on top of what's already
there, save yourself the trouble.

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread galeomaga
Joost Molenaar於 2013年10月5日星期六UTC+8下午7時02分05秒寫道:
> A bit of googling found me this:
> 
> http://www.linux-support.com/cms/implementation-of-tail-in-python/
> 
> 
> 
> import time
> 
> import sys
> 
> 
> 
> def tail_f(file):
> 
>   interval = 1.0
> 
>   while True:
> 
> where = file.tell()
> 
> line = file.readline()
> 
> if not line:
> 
>   time.sleep(interval)
> 
>   file.seek(where)
> 
> else:
> 
>   yield line

After tried many times, updated text file is not shown, it only print text at 
the first time. 

#!/usr/bin/python
import time 
import sys 
import thread

def tail_f(filehandler): 
  interval = 1.0 
  while True: 
try:
line = filehandler.readline()   
where = filehandler.tell()
if not line: 
time.sleep(interval) 
filehandler.seek(where) 
else: 
yield line 
except:
print "tail_f error"


def readfile(systemname):
try:
filehandler = open("/home/martin/Downloads/a.txt","r");
while 1:
#for line in tail_f(filehandler):
#   print line 
try:
interval = 1.0 
line = filehandler.readline()   
where = filehandler.tell()
if not line: 
time.sleep(interval) 
filehandler.seek(where) 
print where
else: 
print line 
except:
print "tail_f error"
except:
print "Error: readfile"

if __name__ == '__main__':
try:
thread.start_new_thread( readfile, ("Thread-1", ) )
except:
print "Error: unable to start thread"

while 1:
pass
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to read list from file

2013-10-05 Thread Roy Smith
In article ,
 Tim Chase  wrote:

> sounds like you want ast.literal_eval():

This sounds like a better idea than either of my earlier suggestions!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to read list from file

2013-10-05 Thread Roy Smith
In article ,
 Harvey Greenberg  wrote:

> I am looping as for L in file.readlines(), where file is csv.
> 
> L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first 
> item is a dir and 2nd is a list, so parsing with split doesn't work.  Is 
> there a way to convert L, which is a string, to the list of 3 items I want?

I hate to recommend it (since it's bad practice for a number of 
legitimate reasons), but passing your string to eval() will get you what 
you want.

It's also very close to being valid JSON syntax, the only difference 
being the use of single instead of double quotes.  You might want to 
just turn it into JSON by substituting the right kind of quotes.

json.loads("[{'a':1, 'b':2}, [1,2,3], 10]".replace("'", '"'))
[{u'a': 1, u'b': 2}, [1, 2, 3], 10]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to read list from file

2013-10-05 Thread Tim Chase
On 2013-10-05 18:08, Harvey Greenberg wrote:
> I am looping as for L in file.readlines(), where file is csv.
> 
> L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that
> the first item is a dir and 2nd is a list, so parsing with split
> doesn't work.  Is there a way to convert L, which is a string, to
> the list of 3 items I want?

sounds like you want ast.literal_eval():

  Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
  [GCC 4.7.2] on linux2
  Type "help", "copyright", "credits" or "license" for more
  information.
  >>> s = "[{'a':1, 'b':2}, [1,2,3], 10]"
  >>> import ast
  >>> print repr(ast.literal_eval(s))
  [{'a': 1, 'b': 2}, [1, 2, 3], 10]

-tkc








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


Re: Database engine bindings for Python (was: Database statements via python but database left intact)

2013-10-05 Thread Chris Angelico
On Sun, Oct 6, 2013 at 12:05 PM, Ben Finney  wrote:
> The Python bindings for MySQL or PostgreSQL, or even SQLite, are tied to
> extension libraries for the specific database engine.
>
> With SQLite this is not a problem for Python's release management,
> because Python's release includes the entire SQLite database engine.
> That code is quite small, so this is deemed a good trade.
>
> With a separately-installed, far more complex database engine like MySQL
> or PostgreSQL, the Python bindings will only work if they are compiled
> against the correct client library.

Hmm. I see what you mean. Of course, that doesn't bind Python to a
specific server version, as one version of the client can talk to any
earlier and many later versions of server, but it is an issue.

It'd probably be unreasonable to package libpq with Windows
installations, but with Linux builds, it should be easy enough to link
against whatever libpq happens to be around. If that's unsuited to
your server version, it's going to be broken for any other libpq-based
apps too. (With package managers like apt, same thing - link against
whatever version can be retrieved easily.)

The alternative is a pure-Python implementation of the PostgreSQL wire
protocol. That would most likely be smaller (if the problem is the
size cost of incorporating all of libpq), but would tie Python's pgsql
module to a specific protocol version (which doesn't change very
often). I don't know if one exists already or not, but it ought to be
possible to port the Pike module [1] to Python, if GPL/LGPL/MPL is
compatible with the Python licensing.

ChrisA

[1] 
http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/Sql/pgsql.html
-- 
https://mail.python.org/mailman/listinfo/python-list


how to read list from file

2013-10-05 Thread Harvey Greenberg
I am looping as for L in file.readlines(), where file is csv.

L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that the first 
item is a dir and 2nd is a list, so parsing with split doesn't work.  Is there 
a way to convert L, which is a string, to the list of 3 items I want?

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


Database engine bindings for Python (was: Database statements via python but database left intact)

2013-10-05 Thread Ben Finney
Chris Angelico  writes:

> It's because of threads like this that I would really like Python to
> nudge people towards something stronger than MySQL. Would it kill
> Python to incorporate PostgreSQL bindings automatically?

I'm not sure what would count as “kill Python”. It would certainly make
the release management of Python needlessly dependent on the release
cycle of an independent project.

The Python bindings for MySQL or PostgreSQL, or even SQLite, are tied to
extension libraries for the specific database engine.

With SQLite this is not a problem for Python's release management,
because Python's release includes the entire SQLite database engine.
That code is quite small, so this is deemed a good trade.

With a separately-installed, far more complex database engine like MySQL
or PostgreSQL, the Python bindings will only work if they are compiled
against the correct client library. That client library is part of the
database engine code release, not Python. So placing that library in
Python's standard library would tie the release of Python's standard
library to the version of the database engine.

I sympathise with the desire to deprecate MySQL and encourage superior
solutions. But your proposed solution would only make Python release
management far more burdensome for an unclear benefit.

-- 
 \  “I tell you the truth: some standing here will not taste death |
  `\ before they see the Son of Man coming in his kingdom.” —Jesus |
_o__) Christ, c. 30 CE, as quoted in Matthew 16:28 |
Ben Finney

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Denis McMahon
On Sat, 05 Oct 2013 16:38:14 +0300, Νίκος Αλεξόπουλος wrote:

> [my cookie code is fucked]

Hi Nick

I had never used python for www before yesterday. I had never used python 
before about 6 months ago.

In the last 24 hours I have installed mod-wsgi on my apache web server, 
and written test code that enables me to see the result of all 
environment data, parse the get string, parse post data, set and parse 
cookies, and save session data to a file specific to the cookie data.

There may be better ways to do some of it, but it works.

Here is a link to the text of my python file:

http://www.sined.co.uk/tmp/index.py.txt

Once you have read through it (there are comments) and understood how it 
handles cookies and session data, you will realise that you can add and 
modify any session data just by adding relevant items to and reading them 
from the session data dictionary. The session data stays on the server, 
the cookie simply identifies the session (and the session data file), and 
hopefully is unique to the user.

Don't ask me any questions about why I did something the way I did it. I 
did it that way because it works. If anyone knows a different way that 
also works and maybe works better, feel free to discuss it. But Nick, 
don't you dare suggest any change that doesn't work because you think it 
looks better.

If you don't understand a module function that I've used, read the python 
documentation for it.

If you think you have a question that is  not covered by the above 
statements, feel free to ask me, here, why I wrote a given line number or 
group of line numbers the way I did. However, see the notes above - the 
answer will probably be "because it works that way".

If you quote the whole file or even large chunks of it here, I am 
finished with trying to help you, ever!

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Database statements via python but database left intact

2013-10-05 Thread Chris Angelico
On Sun, Oct 6, 2013 at 8:39 AM, Ned Batchelder  wrote:
> Now is a good time to go read about transactions, and committing, and the
> difference between MyISAM and InnoDB.  Please don't ask more about it here.

It's because of threads like this that I would really like Python to
nudge people towards something stronger than MySQL. Would it kill
Python to incorporate PostgreSQL bindings automatically?

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Chris Angelico
On Sun, Oct 6, 2013 at 1:31 AM, Νίκος Αλεξόπουλος  wrote:
> # find the visitor record for the (saved) cID and current
> host
> cur.execute('''SELECT * FROM visitors WHERE counterID = %s
> and cookieID = %s''', (cID, cookieID) )
>
> data = cur.fetchone()#cookieID is unique
>
> if not data:
>
> # first time visitor on this page, create new record
> cur.execute('''INSERT INTO visitors (counterID,
> cookieID, host, city, useros, browser, ref, lastvisit) VALUES (%s, %s, %s,
> %s, %s, %s, %s, %s)''', (cID, cookieID, host, city, useros, browser, ref,
> lastvisit) )
> else:
> # found the page, save its primary key for later use
> vID = data[0]
> # UPDATE record using retrieved vID
> cur.execute('''UPDATE visitors SET host = %s, city =
> %s, useros = %s, browser = %s, ref= %s, hits = hits + 1, lastvisit = %s
>
> WHERE counterID = %s and cookieID = %s''', (host, city, useros, browser,
> ref, lastvisit, vID, cookieID) )

Do you understand the expression "race condition", and how it applies
to the above code? If not, you MUST read up on that before relying on
code like this, and as that's not a Python question, I recommend
Google and Wikipedia.[1]

ChrisA

[1] Yes, I know they're not primary sources. For something like this,
tertiary sources are going to do him fine.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Terry Reedy

On 10/5/2013 9:38 AM, Νίκος Αλεξόπουλος wrote:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

...
...

# if browser cookie does not exist, set it
vip = random.randrange(0, 1)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

# first time visitor on this page, create new record
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''', (cID, vip, host, city, useros, browser, ref, lastvisit) )
===
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 


The prefix added to every line of the traceback by the logging process
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
makes the traceback much harder to read. I suggest removing it before 
posting. This is easy with a global replace with nothing. If you control 
the logging and can have the prefix printed just once, even better.


--
Terry Jan Reedy


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


Re: feature requests

2013-10-05 Thread Terry Reedy

On 10/5/2013 11:58 AM, Ethan Furman wrote:

On 10/05/2013 05:49 AM, macker wrote:


Ugly, menial lines are a clue that a function to hide it could be
useful.


Or a clue to add a trivial change elsewhere (hint for Ethan: `return
self` at the end of `Thread.start()`).


I'm aware that would solve your issue.  I'm also aware that Python
rarely does a 'return self' at the end of methods.


Not returning self is a basic design principle of Python since its 
beginning. (I am not aware of any exceptions and would regard one as 
possibly a mistake.) Guido is aware that not doing so prevents chaining 
of mutation methods. He thinks it very important that people know and 
remember the difference between a method that mutates self and one that 
does not. Otherwise, one could write 'b = a.sort()' and not know 
(remember) that b is just an alias for a. He must have seen this type of 
error, especially in beginner code, in other languages before designing 
Python.


> Since that probably isn't going to change,

as it would only make things worse.

Note that some mutation methods also return something useful other than 
default None. Examples are mylist.pop() and iterator.__next__ (usually 
accessed by next(iterator)*. So it is impossible for all mutation 
methods to just 'return self'.


* iterator.__next__ is a generalized specialization of list.pop. It can 
only return the 'first' item, but can do so with any iterable, including 
those that are not ordered and those that represent virtual rather than 
concrete collections.


--
Terry Jan Reedy

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


Re: Database statements via python but database left intact

2013-10-05 Thread Ned Batchelder

On 10/5/13 5:02 PM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 11:31 μμ, ο/η Ian Kelly έγραψε:

Well, have you changed anything in your database configuration?
Whether MySQL uses transactions or not depends on which storage engine
is being used.  I suggest running a test insert with and without
commit to check whether you actually need it or not.



I cannot beleive it!

I have deleted the database and tables and recreted it.
By default it seems to use the InnoDB Engine which wouldn't let 
anythign get inserted/updated.


I then deleted the database recretaed and used at the end fo my create 
table statements the:


create table counters
(
  ID integer(5) not null auto_increment primary key,
  URL varchar(100) not null,
  hits integer(5) not null default 1,
  unique index (URL)
)ENGINE = MYISAM;

After that all mysql queries executed(inserted/updated) properly!

I neved had though of than an engine type could make so much mess.
MyISAM is the way to go then for my web development?
Why InnoDB failed to execute the queries?


Now is a good time to go read about transactions, and committing, and 
the difference between MyISAM and InnoDB.  Please don't ask more about 
it here.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Database statements via python but database left intact

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 11:31 μμ, ο/η Ian Kelly έγραψε:

Well, have you changed anything in your database configuration?
Whether MySQL uses transactions or not depends on which storage engine
is being used.  I suggest running a test insert with and without
commit to check whether you actually need it or not.



I cannot beleive it!

I have deleted the database and tables and recreted it.
By default it seems to use the InnoDB Engine which wouldn't let anythign 
get inserted/updated.


I then deleted the database recretaed and used at the end fo my create 
table statements the:


create table counters
(
  ID integer(5) not null auto_increment primary key,
  URL varchar(100) not null,
  hits integer(5) not null default 1,
  unique index (URL)
)ENGINE = MYISAM;

After that all mysql queries executed(inserted/updated) properly!

I neved had though of than an engine type could make so much mess.
MyISAM is the way to go then for my web development?
Why InnoDB failed to execute the queries?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Benjamin Rovny
On Oct 5, 2013 8:42 AM, "Νίκος Αλεξόπουλος"  wrote:
>
> # initialize cookie
> cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
> cookie.load( cookie )
> vip = cookie.get('ID')
>
> ...
> ...
>
> # if browser cookie does not exist, set it
> vip = random.randrange(0, 1)
> cookie['ID'] = vip
> cookie['ID']['path'] = '/'

Problem here? Randrange returns an integer here, which you are then
treating like a dictionary (hence "Key Error"). That's the extent of my
knowledge though, I don't know about the "cookie" module.

>
> # first time visitor on this page, create new record
> cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''', (cID, vip, host, city, useros, browser, ref, lastvisit) )
> ===
> The above code i wrote gives me the following error:
>
>
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID =
%s''', (cID, vip) )
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100, in execute
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100, in 
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py",
line 650, in escape
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return
escape_item(obj, self.charset)
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py",
line 31, in escape_item
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder =
encoders[type(val)]
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError:

>
> What is the nature of the error?
>   ???
>
> I'll iam trying to do is to give a cookie a random number.
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Database statements via python but database left intact

2013-10-05 Thread Ian Kelly
On Sat, Oct 5, 2013 at 1:36 PM, Νίκος Αλεξόπουλος  wrote:
> Στις 5/10/2013 10:29 μμ, ο/η Zero Piraeus έγραψε:
>>
>> :
>>
>> On Sat, Oct 05, 2013 at 10:16:46PM +0300, Νίκος Αλεξόπουλος wrote:
>>>
>>> Excuse me for asking again today, but i see no error in the
>>> following code, yes no isertion or update happens into the database:
>>>
>>> [...]
>>>
>>>
>>> When this code runs i check instantly my database via PHPMyAdmin and
>>> i see that it was left intact.
>>
>>
>> Are you sure that you're committing your changes (either by having
>> autocommit set or using an explicit con.commit() call)?
>>
>> http://geert.vanderkelen.org/dont-forget-the-commit-in-mysql/
>
>
>
> I dont think that is the issue, because up until now i never used commit and
> all transaction were successfully were happening.

Well, have you changed anything in your database configuration?
Whether MySQL uses transactions or not depends on which storage engine
is being used.  I suggest running a test insert with and without
commit to check whether you actually need it or not.

Also, are you certain that the code is actually being run?  Perhaps
the if condition is evaluating as false and the whole block is being
skipped, or perhaps the code is silently exiting for some reason
before it ever gets to this point.  Do some debugging to determine
what exactly is being executed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Database statements via python but database left intact

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 10:29 μμ, ο/η Zero Piraeus έγραψε:

:

On Sat, Oct 05, 2013 at 10:16:46PM +0300, Νίκος Αλεξόπουλος wrote:

Excuse me for asking again today, but i see no error in the
following code, yes no isertion or update happens into the database:

[...]

When this code runs i check instantly my database via PHPMyAdmin and
i see that it was left intact.


Are you sure that you're committing your changes (either by having
autocommit set or using an explicit con.commit() call)?

http://geert.vanderkelen.org/dont-forget-the-commit-in-mysql/



I dont think that is the issue, because up until now i never used commit 
and all transaction were successfully were happening.


--
What is now proved was at first only imagined! & WebHost

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


Re: Database statements via python but database left intact

2013-10-05 Thread Zero Piraeus
:

On Sat, Oct 05, 2013 at 10:16:46PM +0300, Νίκος Αλεξόπουλος wrote:
> Excuse me for asking again today, but i see no error in the
> following code, yes no isertion or update happens into the database:
> 
> [...]
> 
> When this code runs i check instantly my database via PHPMyAdmin and
> i see that it was left intact.

Are you sure that you're committing your changes (either by having
autocommit set or using an explicit con.commit() call)?

http://geert.vanderkelen.org/dont-forget-the-commit-in-mysql/

 -[]z.

-- 
Zero Piraeus: inter caetera
http://etiol.net/pubkey.asc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Database statements via python but database left intact

2013-10-05 Thread Νίκος Αλεξόπουλος

Actually the whole code is this:

# 
=

# DATABASE INSERTS -
# 
=
if cookieID != 1977 and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:


try:
# locate the ID of the page's URL
cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
data = cur.fetchone()   #URL is unique, so should only 
be one

if not data:
#first time for page; primary key is automatic, hit is 
defaulted
cur.execute('''INSERT INTO counters (url) VALUES 
(%s)''', page )
cID = cur.lastrowid #get the primary key 
value of the new added record
else:
#found the page, save primary key and use it to issue 
hit UPDATE
cID = data[0]
			cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID = %s''', 
cID )



# find the visitor record for the (saved) cID and Cookie
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, cookieID) )

data = cur.fetchone()   #cookieID is unique

if not data:
# first time visitor on this page, create new record
			cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, cookieID, host, city, useros, browser, ref, lastvisit) )

else:
# found the page, save its primary key for later use
vID = data[0]
# UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET host = %s, city = %s, useros = %s, 
browser = %s, ref= %s, hits = hits + 1, lastvisit = %s
		   WHERE counterID = %s and cookieID = %s''', (host, city, useros, 
browser, ref, lastvisit, vID, cookieID) )


except pymysql.ProgrammingError as e:
print( repr(e) )
sys.exit(0)



If at some point an error is made does that mean that no 
update/insertion happens?

PEhats that is why iam seeing no entries at all into my database tables?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Why didn't my threads exit correctly ?

2013-10-05 Thread Piet van Oostrum
李洛  writes:

> Thanks for your reply, MRAB.
>
> I've seen that the child thread has been stoped, it just died in the queue.If 
> I want the queue be
> empty, I must use queue.get() to dequeue and clean it, but I didn't do that.
> I've implement the thread using List now, thanks again.

Also make sure that l_ip and l_result are Queues instead of lists, as MRAB 
suggested.

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Database statements via python but database left intact

2013-10-05 Thread Νίκος Αλεξόπουλος
Excuse me for asking again today, but i see no error in the following 
code, yes no isertion or update happens into the database:



try:
# locate the ID of the page's URL
cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
data = cur.fetchone()   #URL is unique, so should only be one

if not data:
#first time for page; primary key is automatic, hit is defaulted
cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page )
cID = cur.lastrowid #get the primary key 
value of the new added record
else:
#found the page, save primary key and use it to issue hit UPDATE
cID = data[0]
cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID = %s''', 
cID )


When this code runs i check instantly my database via PHPMyAdmin and i 
see that it was left intact.

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


Re: [OT] Re: Hyper-spacial ray-tracer

2013-10-05 Thread Rouslan Korneychuk

On 10/05/2013 11:26 AM, Peter Pearson wrote:

On Fri, 04 Oct 2013 20:17:52 -0400, Rouslan Korneychuk  wrote:
[snip]

I was also wondering about general relativity. I'm not going to go into
too much detail, but basically: if an object with synchronized clocks on
either end of it, passes by a static observer while traveling near the
speed of light, to the outside observer, the object will appear shorter

[snip]

That's special relativity, not general relativity.  Python is
very sensitive to that distinction.



whoops
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 7:56 μμ, ο/η Andreas Perstinger έγραψε:

On 05.10.2013 16:24, Νίκος Αλεξόπουλος wrote:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )


Watch:

 >>> cookie1 = cookies.SimpleCookie('ID=42')
 >>> cookie1.load(cookie1)
 >>> print(cookie1)
Set-Cookie: ID="Set-Cookie: ID=42"
 >>> cookie1.get('ID').value
'Set-Cookie: ID=42'

And now watch this:

 >>> cookie2 = cookies.SimpleCookie('ID=42')
 >>> print(cookie2)
Set-Cookie: ID=42
 >>> cookie2.get('ID').value
'42'

Explanation:

http://docs.python.org/3/library/http.cookies.html#http.cookies.BaseCookie.load


 >>> c = cookies.SimpleCookie('ID=42')
 >>> isinstance(c, dict)
True
 >>> c.items()
dict_items([('ID', )])

Bye, Andreas


Thank you very much Andreas,

it was this strnage behaviour that got me stuch for hours.
Now value gets returned properly.

--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Andreas Perstinger

On 05.10.2013 16:24, Νίκος Αλεξόπουλος wrote:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )


Watch:

>>> cookie1 = cookies.SimpleCookie('ID=42')
>>> cookie1.load(cookie1)
>>> print(cookie1)
Set-Cookie: ID="Set-Cookie: ID=42"
>>> cookie1.get('ID').value
'Set-Cookie: ID=42'

And now watch this:

>>> cookie2 = cookies.SimpleCookie('ID=42')
>>> print(cookie2)
Set-Cookie: ID=42
>>> cookie2.get('ID').value
'42'

Explanation:

http://docs.python.org/3/library/http.cookies.html#http.cookies.BaseCookie.load

>>> c = cookies.SimpleCookie('ID=42')
>>> isinstance(c, dict)
True
>>> c.items()
dict_items([('ID', )])

Bye, Andreas
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 7:42 μμ, ο/η Ned Batchelder έγραψε:


A better solution is to check to see if you got None:

 if cookie.get('ID') is None:
 # make a new cookie


I have tried everythign even wgat you suggested right now:
here is is:

# initialize cookie and retrieve cookie from clients broswer
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )

if cookie.get('ID') is not None:
cookieID = cookie['ID'].value
else:
x = random.randrange(0, 1)
cookie['ID'] = x
cookie['ID']['path'] = '/'
print( cookie )
cookieID = x

print( '''Content-type: text/html; charset=utf-8\n''' )
print( cookieID )

For some reason althogh the cookie does exist in my browser the returnd 
value of cookieI D = cookie['ID'].value is always: Set-Cookie: ID=7482


instead of just the number.
But why?  isnt value to return just the number?

You can see the result of the above code yourself at the top left when 
you visit: http://superhost.gr


it always retursn the whole string instead of just the number...

why can we isolate th damn number only?
in the else i manages to isolate it but not when i try to retrive it.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread MRAB

On 05/10/2013 15:44, Zero Piraeus wrote:

On Sat, Oct 05, 2013 at 05:30:53PM +0300, Νίκος Αλεξόπουλος wrote:

Every mysql statemtns that involved cookieID fails.

in this example this:

# find the visitor record for the (saved) cID and current host
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
cookieID = %s''', (cID, cookieID) )
data = cur.fetchone()#cookieID is unique


If every cur.execute() invocation that you try to pass cookieID to
fails, that suggests you can't pass cookieID to cur.execute() ...
perhaps because it's the wrong type.

The use of '%s' string interpolation suggests that cur.execute() is
expecting a string. Is cookieID a string? If not, is it some kind of
object that contains a string value within it? Maybe it has some kind
of attribute you could pass, like cookieID.value or similar?


MySQL string interpolation uses "%s" as the placeholder, but the value
doesn't have to be a string.


That may or may not be correct, but it's the kind of mental process
you should be going through to solve your problem.


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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder


On 10/5/13 12:17 PM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 7:14 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:

Στις 5/10/2013 7:08 μμ, ο/η Ned Batchelder έγραψε:


On 10/5/13 11:52 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:

 From reading the bottom-most frame, you can see that the 
problem is
that "val" is an http.cookies.Morsel object.  This means you 
probably

tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a 
more

primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a 
random

number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.



[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 84, in 
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID =
cookie.get('ID').value
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]
AttributeError: 'NoneType' object has no attribute 'value'



Nikos: you know enough to understand what is going on here.

This list will not serve you well if you take every error message and
paste it into an email without trying to get to the bottom of it
yourself.  At the very least, a Google search on, "AttributeError:
'NoneType' object has no attribute 'value'" will find you some answers.

I've said it before, I'll say it again:  slow down.

--Ned.



cookieID = cookie.get('ID').value

is not returning what you said it will return

and if cookie.get('ID') doenst exist it returns the error
AttributeError: 'NoneType' object has no attribute 'value'

These are 2 problem.

value aint being returned thw ehole Set-Cookie: ID=some_number is being
returned instead as tou cna see at http://superhost.gr/

and the second problem is

that if the cookie dosnt exist i get the error of: AttributeError:
'NoneType' object has no attribute 'value'

whne this line is tryign to be executed:
cookieID = cookie.get('ID').value

How can i deal with thse 2 problems?


The best solution i cna think of is put the whole thing into a try: block

try:
cookieID = cookie.get('ID').value
except:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
print( cookie )
cookieID = cookie['ID'].value

print( '''Content-type: text/html; charset=utf-8\n''' )

print( cookieID )
sys.exit(0)

That will avoid the NoneType errot but:

that still print out:
Set-Cookie: ID=7413

instead of just the number



Nikos, you are now answering your own emails.  You are going too fast.  
Slow down, think through a solution before writing another email.  And 
seriously, consider IRC, you will be able to have a conversation with 
someone.  The email pace doesn't suit you.


A better solution is to check to see if you got None:

if cookie.get('ID') is None:
# make a new cookie

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: feature requests

2013-10-05 Thread Ethan Furman

On 10/05/2013 05:49 AM, macker wrote:


Ugly, menial lines are a clue that a function to hide it could be useful.


Or a clue to add a trivial change elsewhere (hint for Ethan: `return self` at 
the end of `Thread.start()`).


I'm aware that would solve your issue.  I'm also aware that Python rarely does a 'return self' at the end of methods. 
Since that probably isn't going to change, a helper function is probably your best way forward.




Have you verified that this is a problem in Python?


?


You stated it "would blow up RAM" -- have you actually tested this, or are you making assumptions based on experience 
from other languages, or assumptions based on nothing at all?




You could try subclassing.


I could try many things. What this thread is about is trying to fix it on 
stdlib level, so that people don't have to reinvent the wheel every time.


Did you really expect your idea to just sail through with no opposition, no counter-ideas, no reasons why it might not, 
or would not, work?




Thanks to Chris for his suggestion. Ethan, please stay away from this thread.


Wow, you're rude.

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Zero Piraeus
:

On Sat, Oct 05, 2013 at 05:30:53PM +0300, Νίκος Αλεξόπουλος wrote:
> Every mysql statemtns that involved cookieID fails.
> 
> in this example this:
> 
> # find the visitor record for the (saved) cID and current host
> cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
> cookieID = %s''', (cID, cookieID) )
> data = cur.fetchone()#cookieID is unique

If every cur.execute() invocation that you try to pass cookieID to
fails, that suggests you can't pass cookieID to cur.execute() ...
perhaps because it's the wrong type.

The use of '%s' string interpolation suggests that cur.execute() is
expecting a string. Is cookieID a string? If not, is it some kind of
object that contains a string value within it? Maybe it has some kind of
attribute you could pass, like cookieID.value or similar?

That may or may not be correct, but it's the kind of mental process you
should be going through to solve your problem.

 -[]z.

-- 
Zero Piraeus: inter caetera
http://etiol.net/pubkey.asc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 7:14 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:

Στις 5/10/2013 7:08 μμ, ο/η Ned Batchelder έγραψε:


On 10/5/13 11:52 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.



[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 84, in 
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID =
cookie.get('ID').value
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]
AttributeError: 'NoneType' object has no attribute 'value'



Nikos: you know enough to understand what is going on here.

This list will not serve you well if you take every error message and
paste it into an email without trying to get to the bottom of it
yourself.  At the very least, a Google search on, "AttributeError:
'NoneType' object has no attribute 'value'" will find you some answers.

I've said it before, I'll say it again:  slow down.

--Ned.



cookieID = cookie.get('ID').value

is not returning what you said it will return

and if cookie.get('ID') doenst exist it returns the error
AttributeError: 'NoneType' object has no attribute 'value'

These are 2 problem.

value aint being returned thw ehole Set-Cookie: ID=some_number is being
returned instead as tou cna see at http://superhost.gr/

and the second problem is

that if the cookie dosnt exist i get the error of: AttributeError:
'NoneType' object has no attribute 'value'

whne this line is tryign to be executed:
cookieID = cookie.get('ID').value

How can i deal with thse 2 problems?


The best solution i cna think of is put the whole thing into a try: block

try:
cookieID = cookie.get('ID').value
except:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
print( cookie )
cookieID = cookie['ID'].value

print( '''Content-type: text/html; charset=utf-8\n''' )

print( cookieID )
sys.exit(0)

That will avoid the NoneType errot but:

that still print out:
Set-Cookie: ID=7413

instead of just the number

--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 7:08 μμ, ο/η Ned Batchelder έγραψε:


On 10/5/13 11:52 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.



[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 84, in 
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID =
cookie.get('ID').value
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]
AttributeError: 'NoneType' object has no attribute 'value'



Nikos: you know enough to understand what is going on here.

This list will not serve you well if you take every error message and
paste it into an email without trying to get to the bottom of it
yourself.  At the very least, a Google search on, "AttributeError:
'NoneType' object has no attribute 'value'" will find you some answers.

I've said it before, I'll say it again:  slow down.

--Ned.



cookieID = cookie.get('ID').value

is not returning what you said it will return

and if cookie.get('ID') doenst exist it returns the error 
AttributeError: 'NoneType' object has no attribute 'value'


These are 2 problem.

value aint being returned thw ehole Set-Cookie: ID=some_number is being 
returned instead as tou cna see at http://superhost.gr/


and the second problem is

that if the cookie dosnt exist i get the error of: AttributeError: 
'NoneType' object has no attribute 'value'


whne this line is tryign to be executed:
cookieID = cookie.get('ID').value

How can i deal with thse 2 problems?

--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder


On 10/5/13 11:52 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.



[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 84, in 
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID = 
cookie.get('ID').value
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] 
AttributeError: 'NoneType' object has no attribute 'value'




Nikos: you know enough to understand what is going on here.

This list will not serve you well if you take every error message and 
paste it into an email without trying to get to the bottom of it 
yourself.  At the very least, a Google search on, "AttributeError: 
'NoneType' object has no attribute 'value'" will find you some answers.


I've said it before, I'll say it again:  slow down.

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.



[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 84, in 
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID 
= cookie.get('ID').value
[Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] 
AttributeError: 'NoneType' object has no attribute 'value'


--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 6:06 μμ, ο/η Zero Piraeus έγραψε:

:

On Sat, Oct 05, 2013 at 05:40:23PM +0300, Νίκος Αλεξόπουλος wrote:

When i print CookieID i was under the impression i would see a
random number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"


On Sat, Oct 05, 2013 at 05:47:54PM +0300, Νίκος Αλεξόπουλος wrote:

When i print CookieID i was under the impression i would see a
random number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"


Please don't give identical or near-identical replies to multiple
messages in the thread; other members of the list are either reading all
of your posts or none of them, so repeating yourself like this is only
going to irritate whoever is reading.

Since printing cookieID doesn't produce the output you expect, the
obvious next step is to look up the documentation for whatever kind of
object it is. You can find out its type with

 type(cookieID)

... and then once you know that type (let's say for the sake of argument
it's a Biscuit object), find out about that type of object's attributes
either by googling for the docs or at the interpreter with

 help(Biscuit)

As previously mentioned, there's likely to be some kind of 'value'
attribute that will return just the number you want.

ni...@superhost.gr [~/www/cgi-bin]# python
Python 3.3.2 (default, Aug 26 2013, 06:41:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import os, random
from http import cookies
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )

cookie.load( cookie )

cookieID = cookie.get('ID').value

Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'NoneType' object has no attribute 'value'


And if you go to my webpage http://superhost.gr at the top corner you 
see that allthough i use this code to get the value of the retrieved 
cookie or set the value if ti do


# initialize cookie and retrieve cookie from clients broswer
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID').value

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365  #this cookie will 
expire in a month
print( cookie )
cookieID = cookie['ID'].value

print( '''Content-type: text/html; charset=utf-8\n''' )

print( cookieID )
sys.exit(0)

The output is: Set-Cookie: ID=1376

But how is this possible since we applied the .value attribute in the 
cookie?



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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Andreas Perstinger

On 05.10.2013 17:31, Νίκος Αλεξόπουλος wrote:

Now i have it like this:

# connect to database
con = pymysql.connect( db = 'nikos_metrites', user = 'nikos_root',
passwd = 't1abhp2r!', charset = 'utf8', host = 'localhost' )


Just to be sure: That's not your real password, is it?

Bye, Andreas

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's
browser and if it does nto exist i create one.



For some reason i think CookieID nevers gets inserted itnot the
database that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store
this value.

If iam correct and thi is trully the problem then how can i just get
the random number part out the whole string?

Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

 cookieID = cookie.get('ID')

you actually want this:

 cookieID = cookie.get('ID').value

--Ned.


Thank you Ned, indeed '.value' needed to just print the number.
Now i have it like this:

# connect to database
con = pymysql.connect( db = 'nikos_metrites', user = 'nikos_root', 
passwd = 't1abhp2r!', charset = 'utf8', host = 'localhost' )

cur = con.cursor()

# initialize cookie and retrieve cookie from clients broswer
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )

# if browser cookie does not exist, set it
if not cookie.get('ID'):
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365  #this cookie will 
expire in a month
print( cookie )
cookieID = cookie['ID'].value
# if browser cookie exist, just retrieve it
else:
cookieID = cookie.get('ID').value

and it does not output an error, but for some reason the inserting and 
selecting never happens.


here si the releveant code:


if cookieID != 1977 and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:


try:
# find the visitor record for the (saved) cID and current host
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, cookieID) )

data = cur.fetchone()#cookieID is unique

if not data:
# first time visitor on this page, create new record
			cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, cookieID, host, city, useros, browser, ref, lastvisit) )

else:
# found the page, save its primary key for later use
vID = data[0]
# UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET host = %s, city = %s, useros = %s, 
browser = %s, ref= %s, hits = hits + 1, lastvisit = %s
	WHERE counterID = %s and cookieID = %s''', (host, city, useros, 
browser, ref, lastvisit, vID, cookieID) )


except pymysql.ProgrammingError as e:
print( repr(e) )
sys.exit(0)
=


Any ideas as to what i shoudld try?
the statemnt don't return any error back though and the cookieID is 
indeed now a proper number so i see no reason as to why they fail.


--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Zero Piraeus
:

On Sat, Oct 05, 2013 at 05:40:23PM +0300, Νίκος Αλεξόπουλος wrote:
> When i print CookieID i was under the impression i would see a
> random number like '5369' but instead it display the follwong.
> 
> Set-Cookie: ID="Set-Cookie: ID=5369"

On Sat, Oct 05, 2013 at 05:47:54PM +0300, Νίκος Αλεξόπουλος wrote:
> When i print CookieID i was under the impression i would see a
> random number like '5369' but instead it display the follwong.
> 
> Set-Cookie: ID="Set-Cookie: ID=5369"

Please don't give identical or near-identical replies to multiple
messages in the thread; other members of the list are either reading all
of your posts or none of them, so repeating yourself like this is only
going to irritate whoever is reading.

Since printing cookieID doesn't produce the output you expect, the
obvious next step is to look up the documentation for whatever kind of
object it is. You can find out its type with

type(cookieID)

... and then once you know that type (let's say for the sake of argument
it's a Biscuit object), find out about that type of object's attributes
either by googling for the docs or at the interpreter with

help(Biscuit)

As previously mentioned, there's likely to be some kind of 'value'
attribute that will return just the number you want.

 -[]z.

-- 
Zero Piraeus: post scriptum
http://etiol.net/pubkey.asc
-- 
https://mail.python.org/mailman/listinfo/python-list


[OT] Re: Hyper-spacial ray-tracer

2013-10-05 Thread Peter Pearson
On Fri, 04 Oct 2013 20:17:52 -0400, Rouslan Korneychuk  wrote:
[snip]
> I was also wondering about general relativity. I'm not going to go into 
> too much detail, but basically: if an object with synchronized clocks on 
> either end of it, passes by a static observer while traveling near the 
> speed of light, to the outside observer, the object will appear shorter 
[snip]

That's special relativity, not general relativity.  Python is
very sensitive to that distinction.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will 
expire in a month

cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's 
browser and if it does nto exist i create one.




For some reason i think CookieID nevers gets inserted itnot the 
database that's why mysql's select statemnt fails.


When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.


Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store 
this value.


If iam correct and thi is trully the problem then how can i just get 
the random number part out the whole string?


Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Thanks for being patient.  Where you have this:

cookieID = cookie.get('ID')

you actually want this:

cookieID = cookie.get('ID').value

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder

On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will 
expire in a month

cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's 
browser and if it does nto exist i create one.




For some reason i think CookieID nevers gets inserted itnot the 
database that's why mysql's select statemnt fails.


When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.


Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store 
this value.


If iam correct and thi is trully the problem then how can i just get 
the random number part out the whole string?


Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?




Nikos: stop sending so many emails.  You've asked this question 3 times 
now in this thread.  You aren't even giving people a chance to respond.  
If you want immediate feedback, use IRC.  Email takes a little longer.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 5:43 μμ, ο/η Ned Batchelder έγραψε:

On 10/5/13 10:30 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:

On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
 wrote:

But i have given you the line that produces the error:


The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

ChrisA


Every mysql statemtns that involved cookieID fails.

in this example this:

# find the visitor record for the (saved) cID and current host
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
cookieID = %s''', (cID, cookieID) )
data = cur.fetchone()#cookieID is unique



Nikos, slow down.  Don't post three emails before someone has a chance
to respond.

To get help, you have to show the code that goes along with the
traceback.  Your subject line even says, "select fails", so you know it
is a select statement in the traceback.  You have to show us *that
code*, and more than one line.  You've shown the line here, but we don't
know what cID and cookieID are, so we can't help yet.

Saying "every mysql statement that involves cookieID fails" isn't
enough.  Show us the code containing the line that actually is failing
in that traceback.  Include enough of the code that we can figure out
what is going on.

You've said that you can do better here.  Please try to.

--Ned.


Before we get to the part that iam actually try to insert, select 
releveant records the mysql database we need to make sure that CookieID 
is a number.



# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365#this cookie will 
expire in a month

cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's browser 
and if it does nto exist i create one.




For some reason i think CookieID nevers gets inserted itnot the database 
that's why mysql's select statemnt fails.


When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.


Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store this 
value.


If iam correct and thi is trully the problem then how can i just get the 
random number part out the whole string?


Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?

If its not a number then it will not be selected or inserted properl 
into/from MySQL.

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:


 From reading the bottom-most frame, you can see that the problem is
that "val" is an http.cookies.Morsel object.  This means you probably
tried to use a cookie object as data in your SQL query, and MySQL
doesn't know what to do with that object.  You'll have to use a more
primitive piece of data in your query.


# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365  #this cookie will 
expire in a month
cookieID = cookie.get('ID')
print( cookie )


In the above code i try to retrive the cookie form the visitor's browser 
and if it does nto exist i create one.




For some reason i think CookieID nevers gets inserted itnot the database 
that's why mysql's select statemnt fails.


When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.


Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store this 
value.


If iam correct and thi is trully the problem then how can i just get the 
random number part out the whole string?


Do you see something wrong?
Why cookie['ID'] retuned this string back and not just the number?


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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder

On 10/5/13 10:30 AM, Νίκος Αλεξόπουλος wrote:

Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:

On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
 wrote:

But i have given you the line that produces the error:


The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

ChrisA


Every mysql statemtns that involved cookieID fails.

in this example this:

# find the visitor record for the (saved) cID and current host
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, cookieID) )

data = cur.fetchone()#cookieID is unique



Nikos, slow down.  Don't post three emails before someone has a chance 
to respond.


To get help, you have to show the code that goes along with the 
traceback.  Your subject line even says, "select fails", so you know it 
is a select statement in the traceback.  You have to show us *that 
code*, and more than one line.  You've shown the line here, but we don't 
know what cID and cookieID are, so we can't help yet.


Saying "every mysql statement that involves cookieID fails" isn't 
enough.  Show us the code containing the line that actually is failing 
in that traceback.  Include enough of the code that we can figure out 
what is going on.


You've said that you can do better here.  Please try to.

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:

On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
 wrote:

But i have given you the line that produces the error:


The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

ChrisA


Every mysql statemtns that involved cookieID fails.

in this example this:

# find the visitor record for the (saved) cID and current host
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID 
= %s''', (cID, cookieID) )

data = cur.fetchone()#cookieID is unique

--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 5:24 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:

Στις 5/10/2013 4:59 μμ, ο/η Chris Angelico έγραψε:

On Sat, Oct 5, 2013 at 11:38 PM, Νίκος Αλεξόπουλος
 wrote:

cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''',
(cID, vip, host, city, useros, browser, ref, lastvisit) )
===
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
cookieID =
%s''', (cID, vip) )


No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!


But i have given you the line that produces the error:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
 message  = "ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ ΣΕ
ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!"
 cookie['ID'] = random.randrange(0, 1)
 cookie['ID']['path'] = '/'
 cookie['ID']['expires'] = 60*60*24*365#this cookie will expire
in a month



For some reason i think CookieID nevers gets inserted itnot the database
that's why mysql's select statemnt fails.

When i print CookieID i was under the impression i would see a random
number like '5369' but instead it display the follwong.

Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store this
value.

If iam correct and thi is trully the problem then how can i just get the
random number part out the whole string?


The line is that is failign is any line of any insert update, sleect 
staments that thats cookieID invilded like:


[Sat Oct 05 14:26:24 2013] [error] [client 108.162.229.114]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 219, in 
[Sat Oct 05 14:26:24 2013] [error] [client 108.162.229.114] 
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID 
= %s''', (cID, cookieID) )



--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Chris Angelico
On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
 wrote:
> But i have given you the line that produces the error:

The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

Στις 5/10/2013 4:59 μμ, ο/η Chris Angelico έγραψε:

On Sat, Oct 5, 2013 at 11:38 PM, Νίκος Αλεξόπουλος
 wrote:

cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)''',
(cID, vip, host, city, useros, browser, ref, lastvisit) )
===
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID =
%s''', (cID, vip) )


No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!


But i have given you the line that produces the error:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
cookieID = cookie.get('ID')

# if browser cookie does not exist, set it
if not cookieID:
	message  = "ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ ΣΕ 
ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!"

cookie['ID'] = random.randrange(0, 1)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365  #this cookie will expire in a 
month



For some reason i think CookieID nevers gets inserted itnot the database 
that's why mysql's select statemnt fails.


When i print CookieID i was under the impression i would see a random 
number like '5369' but instead it display the follwong.


Set-Cookie: ID="Set-Cookie: ID=5369"

The mysql filed CookieID is of datatype's int(5) so it cannto store this 
value.


If iam correct and thi is trully the problem then how can i just get the 
random number part out the whole string?





--
What is now proved was at first only imagined! & WebHost

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Chris Angelico
On Sat, Oct 5, 2013 at 11:38 PM, Νίκος Αλεξόπουλος
 wrote:
> cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
> useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)''',
> (cID, vip, host, city, useros, browser, ref, lastvisit) )
> ===
> The above code i wrote gives me the following error:
>
>
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File
> "/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
> [Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
> cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID =
> %s''', (cID, vip) )

No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!

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


Re: Select fails when cookie tried to get a numeric value

2013-10-05 Thread Ned Batchelder

On 10/5/13 9:38 AM, Νίκος Αλεξόπουλος wrote:

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

...
...

# if browser cookie does not exist, set it
vip = random.randrange(0, 1)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

# first time visitor on this page, create new record
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, vip, host, city, useros, browser, ref, lastvisit) )

===
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, vip) )
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 
100, in execute
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
escaped_args = tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 
100, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
escaped_args = tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py", 
line 650, in escape
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return 
escape_item(obj, self.charset)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py", 
line 31, in escape_item
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder = 
encoders[type(val)]
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError: 



What is the nature of the error?
  ???

I'll iam trying to do is to give a cookie a random number.


An important skill to master is reading the traceback for clues.  It 
shows you the lines of code that are involved in the failure.  If you 
read up the stack from the bottom, you can see that bottom four stack 
frames are in the pymysql package.  But the fifth frame is in your code, 
at metrites.py, like 209, executing a select SQL statement.  That's the 
line to focus on.


You haven't shown us that code, but that is where the problem is.

From reading the bottom-most frame, you can see that the problem is 
that "val" is an http.cookies.Morsel object.  This means you probably 
tried to use a cookie object as data in your SQL query, and MySQL 
doesn't know what to do with that object.  You'll have to use a more 
primitive piece of data in your query.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Select fails when cookie tried to get a numeric value

2013-10-05 Thread Νίκος Αλεξόπουλος

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

...
...

# if browser cookie does not exist, set it
vip = random.randrange(0, 1)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

# first time visitor on this page, create new record
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, vip, host, city, useros, browser, ref, lastvisit) )

===
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] 
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID 
= %s''', (cID, vip) )
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", 
line 100, in execute
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args 
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", 
line 100, in 
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args 
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py", 
line 650, in escape
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return 
escape_item(obj, self.charset)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]   File 
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py", 
line 31, in escape_item
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder 
= encoders[type(val)]
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError: 



What is the nature of the error?
  ???

I'll iam trying to do is to give a cookie a random number.
--
https://mail.python.org/mailman/listinfo/python-list


Re: feature requests

2013-10-05 Thread macker
> 
> Ugly, menial lines are a clue that a function to hide it could be useful.

Or a clue to add a trivial change elsewhere (hint for Ethan: `return self` at 
the end of `Thread.start()`).

> Have you verified that this is a problem in Python?

?

> You could try subclassing.

I could try many things. What this thread is about is trying to fix it on 
stdlib level, so that people don't have to reinvent the wheel every time.

Thanks to Chris for his suggestion. Ethan, please stay away from this thread.

-macker



> 
> 
> 
> --
> 
> ~Ethan~

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


Re: Where does MySQLdb put inserted data?

2013-10-05 Thread Mark Lawrence

On 05/10/2013 09:39, F.R. wrote:

On 10/05/2013 12:55 AM, Dennis Lee Bieber wrote:

On Fri, 04 Oct 2013 09:38:41 +0200, "F.R." 
declaimed the following:



MySQLdb, as with all DB-API compliant adapters, does NOT do
"auto-commit" -- you MUST execute a con.commit() after any query
(sequence)
that modifies data. Without it, closing the connection will invoke a
ROLLBACK operation, removing any attempted changes.


That's it! It works! Thank you sooo much. A miracle how I could go
without commits for years and never have missing data. Anyway, another
lesson learned . . .

Thanks

Frederic



Reminds me of the people who've never had an accident but have seen 
thousands in their rear view mirror.  Perhaps the driver here 
http://www.bbc.co.uk/news/uk-england-hampshire-24367601 ?


--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: Where does MySQLdb put inserted data?

2013-10-05 Thread F.R.

On 10/05/2013 12:55 AM, Dennis Lee Bieber wrote:

On Fri, 04 Oct 2013 09:38:41 +0200, "F.R." 
declaimed the following:



MySQLdb, as with all DB-API compliant adapters, does NOT do
"auto-commit" -- you MUST execute a con.commit() after any query (sequence)
that modifies data. Without it, closing the connection will invoke a
ROLLBACK operation, removing any attempted changes.


That's it! It works! Thank you sooo much. A miracle how I could go 
without commits for years and never have missing data. Anyway, another 
lesson learned . . .


Thanks

Frederic

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Mark Lawrence

On 05/10/2013 12:02, Joost Molenaar wrote:

A bit of googling found me this:
http://www.linux-support.com/cms/implementation-of-tail-in-python/

import time
import sys

def tail_f(file):
   interval = 1.0
   while True:
 where = file.tell()
 line = file.readline()
 if not line:
   time.sleep(interval)
   file.seek(where)
 else:
   yield line



In future could you please quote some context so that it's easier for us 
mere mortals to follow the thread, thanks in anticipation.


--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Joost Molenaar
A bit of googling found me this:
http://www.linux-support.com/cms/implementation-of-tail-in-python/

import time
import sys

def tail_f(file):
  interval = 1.0
  while True:
where = file.tell()
line = file.readline()
if not line:
  time.sleep(interval)
  file.seek(where)
else:
  yield line
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Nobody
On Sat, 05 Oct 2013 00:38:51 -0700, galeomaga wrote:

> #!/usr/bin/python
> import time
> f = open('/home/martin/Downloads/a.txt')
> while 1:
>   for line in f:
>   print line;
>   time.sleep(1);

So you're trying to implement "tail -f"?

First, check that "tail -f" actually works for your particular use case.

If the process writing the file uses buffered output, data will only
actually be appended to the file when the buffer is full. You can't read
what isn't there.

And if the process creates a new file with the same name, rather than
appending to the existing file, you'll still be reading the old file. You
would need to open the file again to read the new file.

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Mark Lawrence

On 05/10/2013 09:06, James Harris wrote:

 wrote in message
news:04ee91f9-1cbf-4364-bca3-da25aa4db...@googlegroups.com...



#!/usr/bin/python
import time
f = open('/home/martin/Downloads/a.txt')


Looks like you are on Unix so you can do this from the shell

   tail -F /home/martin/Downloads/a.txt

James




Tail also works on Windows if you've unxutils installed :)

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread Mark Lawrence

On 05/10/2013 08:54, galeom...@gmail.com wrote:


if __name__ == '__main__':
 logfile = open("/home/martin/Downloads/a.txt","r");
 while True:
 line = logfile.readline();
 if not line:
print line;
 time.sleep(1);

this also failed



Usually please state your OS and Python versions, what you expected to 
happen, what actually happened and the full traceback if applicable.  In 
this case I'd hazard a guess that as you're trying to print something 
that evaluates to false you're not likely to see much.  You can also 
remove the semicolons as they're simply not needed.


--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread James Harris
 wrote in message 
news:04ee91f9-1cbf-4364-bca3-da25aa4db...@googlegroups.com...
>
>
> #!/usr/bin/python
> import time
> f = open('/home/martin/Downloads/a.txt')

Looks like you are on Unix so you can do this from the shell

  tail -F /home/martin/Downloads/a.txt

James


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


Re: How to streamingly read text file and display whenever updated text

2013-10-05 Thread galeomaga
gale...@gmail.com於 2013年10月5日星期六UTC+8下午3時38分51秒寫道:
> #!/usr/bin/python
> 
> import time
> 
> f = open('/home/martin/Downloads/a.txt')
> 
> while 1:
> 
>   for line in f:
> 
>   print line;
> 
>   time.sleep(1);


if __name__ == '__main__':
logfile = open("/home/martin/Downloads/a.txt","r");
while True:
line = logfile.readline();
if not line:
print line;
time.sleep(1);

this also failed
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tail recursion to while iteration in 2 easy steps

2013-10-05 Thread Antoon Pardon

Op 04-10-13 23:14, Terry Reedy schreef:

On 10/4/2013 6:46 AM, Ian Kelly wrote:


On the other hand, if you start optimizing every tail call and not
just the recursive functions, then I can see where that could start to
get problematic for debugging -- as arbitrary functions get removed
from the stack traces just because they happened to end in tail calls.


The idea of CPython space-optimizing tail calls when the call is made
has been suggested on python-ideas. Guido verified that it is
technically possible with the current bytecode interpreter but rejected
it because it would arbitrarily mess up stack traces.


What does this mean?

Does it mean that a naive implementation would arbitrarily mess up
stack traces and he wasn't interested in investigating more
sophisticated implementations?

Does it mean he just didn't like the idea a stack trace wouldn't be a
100% represenatation of the active call history?

Does it mean he investigated more sphisticated implementations but found
them to have serious short comings that couldn't be remedied easily?

--
Antoon Pardon
--
https://mail.python.org/mailman/listinfo/python-list


How to streamingly read text file and display whenever updated text

2013-10-05 Thread galeomaga


#!/usr/bin/python
import time
f = open('/home/martin/Downloads/a.txt')
while 1:
for line in f:
print line;
time.sleep(1);
-- 
https://mail.python.org/mailman/listinfo/python-list