Re: 2 + 2 = 5

2012-07-04 Thread Michael Ross

Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin :


I just came across this (https://gist.github.com/1208215):

import sys
import ctypes
pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
five = ctypes.cast(id(5), pyint_p)
print(2 + 2 == 5) # False
five.contents[five.contents[:].index(5)] = 4
print(2 + 2 == 5) # True (must be sufficiently large values of 2  
there...)


Heh.  The author is apparently anonymous, I guess for good reason.



Neat.

Playing with it, i'm wondering:


This:

import sys
import ctypes
pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
five = ctypes.cast(id(5), pyint_p)
five.contents[five.contents[:].index(5)] = 4

print ( 2 + 2 == 5 )
print 5
print 5 - 2

put into a script and run prints:

True
4
3

while entered at the python prompt it prints:

True
4
2

??


Regards,
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to investigate web script not running?

2012-09-28 Thread Michael Ross

On Fri, 28 Sep 2012 13:37:36 +0200, Gilles  wrote:


Hello

I'm trying to run my very first FastCGI script on an Apache shared
host that relies on mod_fcgid:
==
#!/usr/bin/python
from fcgi import WSGIServer
import cgitb

# enable debugging
cgitb.enable()

def myapp(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!\n']

WSGIServer(myapp).run()
==

After following a tutorial, Apache complains with the following when I
call my script:
==
Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.
==



Do it the other way around:

# cgitb before anything else
import cgitb
cgitb.enable()

# so this error will be caught
from fcgi import WSGIServer



Regards,
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tarfile and usernames

2012-12-30 Thread Michael Ross

On Sun, 30 Dec 2012 19:57:31 +0100, Nicholas Cole  wrote:Dear List,I'm hoping to use the tarfile module in the standard library to move some files between computers. I can't see documented anywhere what this library does with userids and groupids.  I can't guarantee that the computers involved will have the same users and groups, and would like the archives to be extracted so that the files are all owned by the extracting user. 
Essentially, I do *not* with to preserve the owner and groups specified in the archives.As long as you don't extract as root, doesn't extracting set the extracting user's uid/gid anyway?Regards,MichaelWhat is the right way to achieve this?
Best wishes,Nicholas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking for valid date input and convert appropriately

2013-02-21 Thread Michael Ross
On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus   
wrote:


Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB  
έγραψε:

On 2013-02-21 19:38, Ferrous Cranus wrote:

> import datetime from datetime



Should be:



from datetime import datetime



>

> try:

>  datetime.strptime( date, '%d %m %Y' )



That parses the date and discards the result.



>  date = date.strptime( '%Y-%m-%d' )



'date' is a string, it doesn't have a 'strptime' method.



When you parsed the date you got a datetime object; _that_ has the

'strptime' method.


I don't need to store the date i just want to make sure is entered  
correctly.

I would like to have the user type the date like

21 02 2013
and then convert it to 2013-02-21 because thats how mysql wants the date  
in order to store it.

Please show me how to write this.

Also, can you show me how to write it that if so even if the user  
entered date is wrong it doesn't just crash the cgi-script?i know i can  
use tr: expect: but i want to avoid it, somehow i need to check the date  
in the same if statemnt like i do with the other user defined varibles  
for validity.




You *have* to try/expect in order to not have the script crash.
Think user typo: 21 02 2ß13

Personally, I'd use a javascript on the html so users can't POST invalid  
dates.
I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13 as  
input,

and you end up with 21.02.2013 posted to your cgi in any case.


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


Re: Checking for valid date input and convert appropriately

2013-02-21 Thread Michael Ross
On Fri, 22 Feb 2013 00:08:01 +0100, Ferrous Cranus   
wrote:


Τη Παρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael  
Ross έγραψε:
On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus  



wrote:



> Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB

> έγραψε:

>> On 2013-02-21 19:38, Ferrous Cranus wrote:

>>

>> > import datetime from datetime

>>

>>

>>

>> Should be:

>>

>>

>>

>> from datetime import datetime

>>

>>

>>

>> >

>>

>> > try:

>>

>> >  datetime.strptime( date, '%d %m %Y' )

>>

>>

>>

>> That parses the date and discards the result.

>>

>>

>>

>> >  date = date.strptime( '%Y-%m-%d' )

>>

>>

>>

>> 'date' is a string, it doesn't have a 'strptime' method.

>>

>>

>>

>> When you parsed the date you got a datetime object; _that_ has the

>>

>> 'strptime' method.

>

> I don't need to store the date i just want to make sure is entered

> correctly.

> I would like to have the user type the date like

>

> 21 02 2013

> and then convert it to 2013-02-21 because thats how mysql wants the  
date


> in order to store it.

> Please show me how to write this.

>

> Also, can you show me how to write it that if so even if the user

> entered date is wrong it doesn't just crash the cgi-script?i know i  
can


> use tr: expect: but i want to avoid it, somehow i need to check the  
date


> in the same if statemnt like i do with the other user defined varibles

> for validity.

>



You *have* to try/expect in order to not have the script crash.

Think user typo: 21 02 2ß13



Personally, I'd use a javascript on the html so users can't POST invalid

dates.

I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13  
as


input,

and you end up with 21.02.2013 posted to your cgi in any case.


i just want to check for date validity from within ha same if statemnt  
like i check the other variables and i tried a regualr expression just  
now:


if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and  
( date and re.search( r'(\d+) (\d+) (\d+)', date ) ) ):


Do i ahve somehting wrong in it?

will the if become true if the user enters 21 02 2013 ?



I pretend not to know, try it out?

And try with invalid inputs, too:
211 02 2013
21 99 2013
31 02 2013
--
http://mail.python.org/mailman/listinfo/python-list


Re: Checking for valid date input and convert appropriately

2013-02-21 Thread Michael Ross
On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus   
wrote:



Please i have been trying hours for this:


Don't do that: Spending hours on being stuck. Take a break. Call it a  
night.

Brain needs time to unstick itself.


Besides:


from datetime import date



entry='31 03 2013'



day, month, year = int(entry[:2]), int(entry[3:5]), int(entry[6:])



mydate=date(year,month,day)



mydate.strftime('%Y-%m-%d')

'2013-03-31'





try:
datetime.strptime(date, '%d m% %Y')
date = date.strftime('%Y-%m-%d')
except:
print( "date not propetly entered." )
sys.exit(0)
===

the user enters 21 02 2013

a) i just need to check if its in accepted format
b) then truncate the given date to mysql format

That's all i want to do and i cant make it work. Plese tell me how to  
write this.


I have tried anything i can think of.

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


Re: Python Newbie

2013-02-24 Thread Michael Ross

On Sun, 24 Feb 2013 20:40:05 +0100,  wrote:


> if (some statement):   # short form
>
> rather than
>
> if (some statement == true):   # long form


What all those ugly brackets are for?



Mark,

Back in the day when C was king, or take many newer long established  
languages (C#, Java), the use of () has been widespread and mandated by  
the compilers. I have never heard anyone moan about the requirement to  
use parentheses.


You've never heard me then. I ... "strongly dislike" having to parse  
visual elements which I consider superfluous and implicit.


Does the English language have a proverb like "not being able to see the  
forest for the trees"?


To me, a C source looks like all brackets. Can't see the code for all the  
brackets.



Now come Python in which parens are optional, and all of a sudden they  
are considered bad and apparently widely abandoned. Do you really not  
see that code with parens is much more pleasing visually?


I guess one can get just as religious about the brackets as one can about  
the whitespace.



if ( condition ) { action }
vs
if condition: action


In time estimated, I'd say I can read and understand Python code about 20%  
faster than any of these brackety languages, even compared to languages I  
worked a with couple of years longer. That's a lot of effort saved.




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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-05 Thread Michael Ross
On Tue, 05 Mar 2013 21:04:59 +0100, Νίκος Γκρ33κ   
wrote:



#open html template
if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )

htmldata = f.read()
		counter = ''' mailto:supp...@superhost.gr";> src="/data/images/mail.png"> 

  
  Αριθμός 
Επισκεπτών
	  http://superhost.gr/?show=stats";>  
%d 

  ''' % data[0]

#render template
template = htmldata + counter
print ( template )

Yes the aboev code does work if we talk about appending html data to an  
already html file!


But if the file is some_python.py file then i cannot just append the  
data.
Actually by appending i dont want to actually insert the data to the end  
of the .py file, thus altering it but instead run the .py file and print  
the counter html data afterwards!



subprocess.checkoutput() ?

if htmlpage.endswith('.py'):
htmldata=subprocess.check_output(...)
counter=...
template=htmldata+counter

http://docs.python.org/2/library/subprocess.html?highlight=check_output#subprocess.check_output




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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-05 Thread Michael Ross
On Tue, 05 Mar 2013 23:47:18 +0100, Νίκος Γκρ33κ   
wrote:


Thank you very much! This is what i was looking for and here is my code  
after receiving your help.
So, with the command you provided to me i can actually run the .py  
script ans save its output and then append from there!! Great!


Here is my code now!

if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )
htmldata = f.read()
elif htmlpage.endswith('.py'):
		htmldata = subprocess.check_output( open(  
"/home/nikos/public_html/cgi-bin/" + htmlpage ) )



	counter = ''' mailto:supp...@superhost.gr";> src="/data/images/mail.png"> 

  
Αριθμός Επισκεπτών
http://superhost.gr/?show=stats";> %d  


  ''' % data[0]


template = htmldata + counter
print ( template )
===

But i'am getting this error:
: 'module' object has no attribute  
'check_output'


Why does it say it has no attribute?


Python version < 2.7 ?

And it's more along the lines of
subprocess.check_output( '/home/nikos/.../' + htmlpage )
without "open".
Or even
	subprocess.check_output( [ '/your/python/interpreter', '/home/nikos/...'  
] )

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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-05 Thread Michael Ross
On Wed, 06 Mar 2013 00:39:31 +0100, Νίκος Γκρ33κ   
wrote:


htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' +  
htmlpage )


htmldata = subprocess.check_output( ['/usr/bin/python',  
'/home/nikos/public_html/cgi-bin/' + htmlpage] )


Both of the above statemnts fail i'am afraid with the same error message.



check_output is available as of Python 2.7

I guess you are still on version 2.6 ?


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


Re: sync databse table based on current directory data without losign previous values

2013-03-06 Thread Michael Ross
On Wed, 06 Mar 2013 12:52:00 +0100, Mark Lawrence  
 wrote:



On 06/03/2013 07:45, Νίκος Γκρ33κ wrote:
I'am using this snipper to read a current directory and insert all  
filenames into a databse and then display them.


But what happens when files are get removed form the directory?
The inserted records into databse remain.
How can i update  the databse to only contain the existing filenames  
without losing the previous stored data?


Here is what i ahve so far:

==
path = "/home/nikos/public_html/data/files/"

#read the containing folder and insert new filenames
for result in os.walk(path):


You were told yesterday at least twice that os.walk returns a tuple but  
you still insist on refusing to take any notice of our replies when it  
suits you, preferring instead to waste everbody's time with these  
questions.  Or are you trying to get into the Guinness Book of World  
Records for the laziest bastard on the planet?


Hold on a sec ...

He has

for result in os.walk(path):
for filename in result[2]:

So he *did* take notice of that.


Nikos:
Expectation is to iterate through a tuple like this:

for dirpath, dirnames, filenames in os.walk(path):
...







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


Re: Inserting-embedding some html data at the end of a .py file

2013-03-06 Thread Michael Ross
On Thu, 07 Mar 2013 00:18:44 +0100, Νίκος Γκρ33κ   
wrote:


Τη Τετάρτη, 6 Μαρτίου 2013 2:06:33 π.μ. UTC+2, ο χρήστης Michael Ross  
έγραψε:



check_output is available as of Python 2.7
I guess you are still on version 2.6 ?



I can access each of these from my jailed shell user account without  
issue, and especially i try /usr/bin/python3


ni...@superhost.gr [~]# /usr/bin/python -V
Python 2.6.6

ni...@superhost.gr [~]# /opt/python3/bin/python3 -V
Python 3.2.3

ni...@superhost.gr [~]# /usr/bin/python3 -V
Python 3.2.3


Problem is that when i change my shebang constructor to  
#!/sur/bin/python3 my '/cgi-bin/metrites.py' python cgi scripts produces  
an internal server error while with #!/sur/bin/python the script works.


Any ideas as to why? I can post ocde if you want to.



You need to post the Traceback.

Either run /usr/bin/python3 /cgi-bin/metrites.py on the shell
or better look in your webserver error log.

Guess:
In Python 3 "print" is a function.
So
print "something"
will not work. You need to
print("something")
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do you feel bad because of the Python docs?

2013-03-06 Thread Michael Ross
On Thu, 07 Mar 2013 02:28:10 +0100, Chris Kaynor  
 wrote:



I actually just tried that, and the results weren't very good.

Using the doc's search feature, the "Reporting Bugs" (and the "About  
these documents") page >was significantly down the page (about 2/3 of  
the way) - not the most obvious result in the pile. All the >other  
searches I could think of either didn't return either of those pages at  
all, or they were also 1/2-2/3 >of the way down the page.





Using Google it took me about 3 seconds to find the page.

"python report doc bug".

Works with Bing or DuckDuckGo too. First hit on either engine.

The doc's search is ... fine if I search for e. g. 'subprocess.Popen',
but near-useless for general searches.




The link is also on the main documentation page, but it is, again, near  
the bottom, making it hidden from >many users. Both of them show up just  
above the bottom of the window with it maximized on my screen.




The first of the issues may be difficult to fix,


Is it?
You'd just have to have an additional search box labeld "search whole page  
with google/bing/whatever" (?)





but would likely be fairly useful - that would generally be my first  
port of call. The second one is more >minor as most people will scroll  
down to see whats farther down, if they go to the main page to find the  
>links.




Chris



On Wed, Mar 6, 2013 at 5:06 PM, alex23  wrote:



On Mar 7, 10:47 am, Rick Johnson  wrote:


That's great Terry, but how will the next person find the link?




Why do you have such a low opinion of others that you think they're

unable to look up "Reporting Bugs" in the _documentation_?

--

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




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


Re: An error when i switched from python v2.6.6 => v3.2.3

2013-03-07 Thread Michael Ross
On Thu, 07 Mar 2013 12:27:03 +0100, Νίκος Γκρ33κ   
wrote:


Τη Πέμπτη, 7 Μαρτίου 2013 11:06:27 π.μ. UTC+2, ο χρήστης Νίκος Γκρ33κ  
έγραψε:

Any ideas about the error please?



I can assure you all the statemnt are correct ebcause they work in  
python v2.6.6


can someone help this issue so my webiste will get back on working plz?


MySQLdb is python 2 only.
"MySQL-3.23 through 5.0 and Python-2.3 through 2.7 are currently  
supported. Python-3.0 will be supported in a future release."


Maybe try this one:
https://pypi.python.org/pypi/mysql-connector-python/1.0.9

Alternatively,
forget about subprocess.check_output,
call your metrites.py with os.system('python metrites.py some-temp-file')
and have it write its output to some-temp-file,
and read some-temp-file back into your template?

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


Re: An error when i switched from python v2.6.6 => v3.2.3

2013-03-07 Thread Michael Ross
On Thu, 07 Mar 2013 13:25:58 +0100, Νίκος Γκρ33κ   
wrote:


Τη Πέμπτη, 7 Μαρτίου 2013 1:51:42 μ.μ. UTC+2, ο χρήστης Michael Ross  
έγραψε:

On Thu, 07 Mar 2013 12:27:03 +0100, Νίκος Γκρ33κ 

wrote:



> Τη Πέμπτη, 7 Μαρτίου 2013 11:06:27 π.μ. UTC+2, ο χρήστης Νίκος Γκρ33κ

> έγραψε:

>> Any ideas about the error please?

>>

>>

>>

>> I can assure you all the statemnt are correct ebcause they work in

>> python v2.6.6

>

> can someone help this issue so my webiste will get back on working  
plz?




MySQLdb is python 2 only.

"MySQL-3.23 through 5.0 and Python-2.3 through 2.7 are currently

supported. Python-3.0 will be supported in a future release."



Maybe try this one:

https://pypi.python.org/pypi/mysql-connector-python/1.0.9



Alternatively,

forget about subprocess.check_output,

call your metrites.py with os.system('python metrites.py  
some-temp-file')


and have it write its output to some-temp-file,

and read some-temp-file back into your template?


Yes its better for me to give upon python3 for the moment being, because  
all that hassle was for makeing the subprocess command run.


Now i just followed your advice and tried:

elif htmlpage.endswith('.py'):
		htmldata = os.system('/usr/bin/python ./metrites.py  
/data/files/htmltemp')


but produced error tells me that:

template undefined, htmldata = -1, counter = ' href="mailto:supp...@superhost.gr";> ...st.gr/?show=stats">color=cyan> 0 \n\t\t '


whay htmldata reeturn the value '-1' when os.sytem is executed?


Because
"On Unix, the return value is the exit status of the process encoded in  
the format specified for wait()."


It's in the documentation for os.system.
Which you should have read before asking here.

You may not intend it like that,
but if you ask any and all errors here, you effectively ask the list to  
develop the software for you.


Seriously this is beyond the scope of volunteer help for a commercial site.
I'm bailing out.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Excel column 256 limit

2013-03-18 Thread Michael Ross
On Mon, 18 Mar 2013 16:50:21 +0100, Steven D'Aprano  
 wrote:



On Mon, 18 Mar 2013 08:28:46 -0700, Ana Dionísio wrote:


Is there some way to go around this limit? I need to import data from
python to excel and I need 1440 columns for that.


That's an Excel question, it has nothing to do with Python.

Have you considered using something other than Excel? As I understand it,
OpenOffice, LibreOffice, and Gnumeric do not have a 256 column limit.


Just for completeness:
Excel in it's "Office 2010" version does not have a 256 column limit  
either.

I can use > 2000 columns without problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Excel column 256 limit

2013-03-19 Thread Michael Ross

On Tue, 19 Mar 2013 15:07:54 +0100, Neil Cerutti  wrote:


On 2013-03-18, Ana Dion?sio  wrote:

But I still get the error and I use Excel 2010.

I'm trying to export data in a list to Excel


xlrd: Library for developers to extract data from Microsoft Excel
(tm).

It is for *reading* Excel files, not writing them. To get data
into Excel use the csv module and create the file using the
default 'excel-csv' format. Then load the file using Excel.

Creating an Excel file directly in Python is possible, but I
think it will require use of the Pywin32 extensions.



I use and recommend http://pythonhosted.org/openpyxl/ for creating Excel  
files from Python.

No trouble so far.
--
http://mail.python.org/mailman/listinfo/python-list