Re: using split for a string : error

2013-01-25 Thread Hans Mulder
On 25/01/13 15:04:02, Neil Cerutti wrote:
 On 2013-01-25, Oscar Benjamin oscar.j.benja...@gmail.com wrote:
 On 24 January 2013 11:35, Chris Angelico ros...@gmail.com wrote:
 It's usually fine to have int() complain about any
 non-numerics in the string, but I must confess, I do sometimes
 yearn for atoi() semantics: atoi(123asd) == 123, and
 atoi(qqq) == 0. I've not seen a convenient Python function
 for doing that. Usually it involves manually getting the
 digits off the front. All I want is to suppress the error on
 finding a non-digit. Oh well.

 I'm interested to know what the situations are where you want
 the behaviour of atoi().
 
 Right. atoi is no good even in C. You get much better control
 using the sprintf family.

I think you meant sscanf.

It's true that sscanf gives you more control.  That being said,
sometimes the one option atoi gives you, just happens to be what
you need.

 int would need to return a tuple of the
 number it found plus the number of characters consumed to be more
 useful for parsing.
 
 intparse(123abc)
 (123, 3)
 
 But that would make it might inconvenient for general use.

If the new function is nameed intparse, and the existing int
function remains available, then most use cases would be served
by int, and intparse would be available as a building block for
other use cases.  For example atoi could be defined as:

def atoi(s): return intparse(s)[0]

intparse(xyz) should return (0, 0), and leave it to the caller
to decide whether a ValueError shoud be raised.


-- HansM




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


Re: urllib2 FTP Weirdness

2013-01-23 Thread Hans Mulder
On 24/01/13 00:58:04, Chris Angelico wrote:
 On Thu, Jan 24, 2013 at 7:07 AM, Nick Cash
 nick.c...@npcinternational.com wrote:
 Python 2.7.3 on linux

 This has me fairly stumped. It looks like
 urllib2.urlopen(ftp://some.ftp.site/path;).read()
 will either immediately return '' or hang indefinitely. But
 response = urllib2.urlopen(ftp://some.ftp.site/path;)
 response.read()
 works fine and returns what is expected. This is only an issue with urllib2, 
 vanilla urllib doesn't do it.

 The site I first noticed it on is private, but I can reproduce it with 
 ftp://ftp2.census.gov/;.
 
 Confirmed on 2.6.5 on Windows, fwiw. This is extremely weird.

It works fine with 2.7.3 on my Mac.

 Possibly it's some kind of race condition??

If urllib2 is using active mode FTP, then a firewall on your box
could explain what you're seeing.  But then, that's why active
mode is hardly used these days.


Hope this helps,

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


Re: help

2013-01-11 Thread Hans Mulder
On 10/01/13 19:35:40, kwakukwat...@gmail.com wrote:
 pls this is a code to show the pay of two people.bt I want each of to be
 able to get a different money when they enter their user name,and to use
 it for about six people.
 database = [
 ['Mac'],
 ['Sam'],
 ]
 pay1 = 1000
 pay2 = 2000
  
 name = raw_input('Enter your name: ')
 if [name] in database:print your pay is $,pay

How about:

database = dict(Mac=1000, Sam=2000, Eric=3000, Terry=4000, Graham=5000)

name = raw_input('Enter your name: ')
if name in database: print Your pay is ${}.format(database[name])


Hope this helps,

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


Re: please i need explanation

2013-01-11 Thread Hans Mulder
On 11/01/13 16:35:10, kwakukwat...@gmail.com wrote:
 def factorial(n):
 if n2:
  return 1
 f = 1
 while n= 2:
 f *= n
 f -= 1

U think this line should have been:

  n -= 1

 return f


Hope this helps,

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


Re: Numpy outlier removal

2013-01-06 Thread Hans Mulder
On 6/01/13 20:44:08, Joseph L. Casale wrote:
 I have a dataset that consists of a dict with text descriptions and values 
 that are integers. If
 required, I collect the values into a list and create a numpy array running 
 it through a simple
 routine: data[abs(data - mean(data))  m * std(data)] where m is the number 
 of std deviations
 to include.
 
 
 The problem is I loos track of which were removed so the original display of 
 the dataset is
 misleading when the processed average is returned as it includes the removed 
 key/values.
 
 
 Ayone know how I can maintain the relationship and when I exclude a value, 
 remove it from
 the dict?

Assuming your data and the dictionary are keyed by a common set of keys:

for key in descriptions:
if abs(data[key] - mean(data)) = m * std(data):
del data[key]
del descriptions[key]


Hope this helps,

-- HansM

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


Re: Missing something obvious with python-requests

2013-01-04 Thread Hans Mulder
On 4/01/13 03:56:47, Chris Angelico wrote:
 On Fri, Jan 4, 2013 at 5:53 AM, Ray Cote
 rgac...@appropriatesolutions.com wrote:
 proxies = {
 'https': '192.168.24.25:8443',
 'http': '192.168.24.25:8443', }

 a = requests.get('http://google.com/', proxies=proxies)


 When I look at the proxy log, I see a GET being performed -- when it should 
 be a CONNECT.
 Does not matter if I try to get http or https google.com.

 Not sure if it's related to your problem or not, but my understanding
 of a non-SSL request through a proxy is that it'll be a GET request
 (eg GET http://google.com/ HTTP/1.0). So the problem is only that
 it's still doing GET requests when it's an https query (which is where
 CONNECT is needed).

It all depends on the proxy URL.

It the proxy URL is http://192.168.24.25/, then the client should send
GET requests to the proxy in both cases, and the proxy should send GET
or CONNECT to the origin server, depending on whether origin URL uses
SSL.

If the proxy URL is https://192.168.24.25/, then the client should send
CONNECT requests to the proxy, and the proxy should send GET or CONNECT
as appropriate.

Python-requests appears to implement only the first case.


Hope this helps,

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


Re: New to python, do I need an IDE or is vim still good enough?

2012-12-31 Thread Hans Mulder
On 31/12/12 12:57:59, Adam Tauno Williams wrote:
 On Thu, 2012-12-27 at 12:01 -0800, mogul wrote:
 'Aloha!
 I'm new to python, got 10-20 years perl and C experience, all gained
 on unix alike machines hacking happily in vi, and later on in vim.
 Now it's python, and currently mainly on my kubuntu desktop.
 Do I really need a real IDE, as the windows guys around me say I do,
 
 You don't need one.

+1

 You are crazy if you don't WANT one.

ITYM: you'd be crazy if you'd want one.

 Check out geany http://www.geany.org/

Don't bother: Python comes with a free IDE named IDLE.  Try it
for a few minutes, and you'll find that most of the features
that make vim so wonderful, are missing from IDLE.

Just stay with vim.

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


Re: Question about nested loop

2012-12-31 Thread Hans Mulder
On 31/12/12 11:02:56, Isaac Won wrote:
 Hi all,
 I am a very novice for Python. Currently, I am trying to read continuous
 columns repeatedly in the form of array. 
 my code is like below:
 
 import numpy as np
 
 b = [] 
 c = 4
 f = open(text.file, r)
 
 while c  10:
 c = c + 1
 
 for  columns in ( raw.strip().split() for raw in f ):
 b.append(columns[c])
 
 y = np.array(b, float)
 print c, y
 
 
 I thought that  can get the arrays of the columns[5] to [10], but I only
 could get repetition of same arrays of columns[5].
 
 The result was something like:
 
 5 [1 2 3 4 .., 10 9 8]
 6 [1 2 3 4 .., 10 9 8]
 7 [1 2 3 4 .., 10 9 8]
 8 [1 2 3 4 .., 10 9 8]
 9 [1 2 3 4 .., 10 9 8]
 10 [1 2 3 4 .., 10 9 8]
 
 
 What I can't understand is that even though c increased incrementally upto 10,
 y arrays stay same.
 
 Would someone help me to understand this problem more?

That's because the inner loop read from a file until his reaches
the end of the file.  Since you're not resetting the file pointer,
during the second and later runs of the outer loop, the inner loop
starts at the end of the file and terminates without any action.

You'd get more interesting results if you rewind the file:

import numpy as np

b = []
c = 4
f = open(text.file, r)

while c  10:
c = c + 1

f.seek(0,0)
for  columns in ( raw.strip().split() for raw in f ):
b.append(columns[c])

y = np.array(b, float)
print c, y

It's a bit inefficient to read the same file several times.
You might consider reading it just once.  For example:

import numpy as np

b = []

f = open(text.file, r)
data = [ line.strip().split() for line in f ]
f.close()

for c in xrange(5, 11):
for row in data:
b.append(row[c])

y = np.array(b, float)
print c, y


Hope this helps,

-- HansM



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


Re: Tarfile and usernames

2012-12-30 Thread Hans Mulder
On 30/12/12 19:57:31, 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.
 
 What is the right way to achieve this?

I would agree that this ought to be documented.

From reading the code: the way to achieve this, is to run as a user
other than root. Or monkeypatch the TarFile.chown method to be a no-op.


Hope this helps,

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


Re: Python lists

2012-12-30 Thread Hans Mulder
On 28/12/12 18:46:45, Alex wrote:
 Manatee wrote:
 
 On Friday, December 28, 2012 9:14:57 AM UTC-5, Manatee wrote:
 I read in this:

 ['C100, C117', 'X7R 0.033uF 10% 25V  0603', '0603-C_L, 0603-C_N',
 '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
 'Digi-Key', '490-1521-1-ND', '']



 Then I need to convert it to this:

 [['C100', 'C117'], 'X7R 0.033uF 10% 25V  0603', '0603-C_L',
 '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA',
 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]

 
 l = ['C100, C117', 'X7R 0.033uF 10% 25V  0603', '0603-C_L, 0603-C_N',
 '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
 'Digi-Key', '490-1521-1-ND', '']
 
 [item.split(',') if i == 0 else item for i, item in enumerate(l)]

If it's okay to modify the original list, you can simply do:

l[0] = split(l[0], , )

If modifying the original is not okay, the simple solution would
be to copy it first:

l2 = l
l2[0] = split(l2[0], , )


Hope this helps,

-- HansM

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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Hans Mulder
Hello,

Python does not support REAL numbers.  It has float number, which
are approximations of real numbers.  They behave almost, but not
quite, like you might expect.

It also has Decimal numbers.  They also approximate real numbers,
but slightly differently.  They might behave more like you'd expect
if you're used to an electronic calculator.


On 30/12/12 23:37:53, Alvaro Lacerda wrote:
 The code I wrote is supposed to ask the user to enter a number;
 Then tell the user what's going to happen to that number (x / 2 + 5) ;
 Then give the user an answer;
 
 I succeeded getting results from even numbers, but when I try diving
 an uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2) 
 
 I'm trying to get full number result using the %d command, I've tried
 to add the line from __future__ import division to the beginning of
 my code, but I haven't been succeeded.
 
 I also tried making my numbers decimals (i.e. 2.0 instead of just 2)

Errrhm, 2.0 is a float.

To get decimal numbers, you'd need to import the Decimal module.



 Below is my program and thanks for the help :)
 
 
 
 from __future__ import division;
 
 def decimal_percent_test():
 number = input(Enter a number: );
 print number, will be divided by 2 then added by 5!;
 print  %d divided by %d is %d plus %d is... %d ! %(
   number,2,number/2,5,number/2+5);

The %d code will convert your number to a whole number.
as you found out.

The simplest way to make this program work, is to use the %s
code, which doesn't convert to a different type of number:

from __future__ import division;

def decimal_percent_test():
number = input(Enter a number: )
print number, will be divided by 2 then added by 5!
print  %s divided by %s is %s plus %s is... %s ! % (
number, 2, number/2, 5, number/2+5)

while True:
decimal_percent_test()


Enter a number: 10
10 will be divided by 2 then added by 5!
 10 divided by 2 is 5.0 plus 5 is... 10.0 !
Enter a number: 11
11 will be divided by 2 then added by 5!
 11 divided by 2 is 5.5 plus 5 is... 10.5 !
Enter a number: x
Traceback (most recent call last):
  File test.py, line 11, in module
decimal_percent_test()
  File test.py, line 5, in decimal_percent_test
number = input(Enter a number: )
  File string, line 1, in module
NameError: name 'x' is not defined



Hope this helps,

-- HansM

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


Re: Python lists

2012-12-30 Thread Hans Mulder
On 30/12/12 23:25:39, Evan Driscoll wrote:
 On 12/30/2012 4:19 PM, Hans Mulder wrote:
 If it's okay to modify the original list, you can simply do:

 l[0] = split(l[0], , )

 If modifying the original is not okay, the simple solution would
 be to copy it first:

 l2 = l
 l2[0] = split(l2[0], , )
 
 Um, that doesn't copy the list:

Oops, you're right.


 l = [C100, C117, X7R ...]
 l2 = l
 import string
 l2[0] = string.split(l2[0], , )
 l
 [['C100', 'C117'], 'X7R ...']
 l2
 [['C100', 'C117'], 'X7R ...']
 
 To make a copy of a list you can either use slice syntax (l2 = l[:]) or
 call the list constructor (l2 = list(l)).

Thanks for correcting me,

-- HansM

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


Re: need a url that its http response header that cotain 401 status code

2012-12-26 Thread Hans Mulder
On 26/12/12 10:08:41, iMath wrote:
 I am going to do a  Basic Authentication ,
 so I need a url 
  that its http response header that cotain 401 status code.

Isn't that backwards?  I mean, what's the point of implementing
Basic Authentication, unless you already know a site that uses it?

if you don't know any such site, you can set one up easily:
just run a webserver on your laptop and password-protect a page
in your server configuration.

When you've found a suitable url, take a look at HTTPBasicAuthHandler
in the urllib2 modules.


Hope this helps,

-- HansM


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


Re: how to detect the character encoding in a web page ?

2012-12-23 Thread Hans Mulder
On 24/12/12 01:34:47, iMath wrote:
 how to detect the character encoding  in a web page ?

That depends on the site: different sites indicate
their encoding differently.

 such as this page:  http://python.org/

If you download that page and look at the HTML code, you'll find a line:

  meta http-equiv=content-type content=text/html; charset=utf-8 /

So it's encoded as utf-8.

Other sites declare their charset in the Content-Type HTTP header line.
And then there are sites relying on the default.  And sites that get
it wrong, and send data in a different encoding from what they declare.


Welcome to the real world,

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


Re: Forking into the background (Linux)

2012-12-23 Thread Hans Mulder
On 24/12/12 01:50:24, Olive wrote:
 My goal is to write a script that 1) write something to stdout; then
 fork into the background, closing the stdout (and stderr, stdin) pipe.
 
 I have found this answer (forking - setsid - forking)
 http://stackoverflow.com/a/3356154
 
 However the standard output of the child is still connected to the
 terminal. I would like that if we execute a subprocess.checkprocess on
 this program,  only I would like to see this is captured and that the
 program terminates when the parent exits.
 
 #! /usr/bin/python2
 import os,sys,time
 
 print I would like to see this
 pid = os.fork()
 if (pid == 0): # The first child.
 # os.chdir(/)
os.setsid()
# os.umask(0)
pid2 = os.fork()
if (pid2 == 0):  # Second child
  print I would like not see this
  time.sleep(5)
else:
  sys.exit()#First child exists
 else:   # Parent Code
   sys.exit()   # Parent exists

You could do this before forking:

sys.stdin.close()
sys.stdin = open('/dev/null', 'r')
sys.stdout.close()
sys.stdout = open('/dev/null', 'w')
sys.stderr.close()
sys.stderr = open('/dev/null', 'w')


You may want to look at the python-daemon module on Pypy, which appears
to do what you need, including some features you haven't asked for, yet.


Hope this helps,

-- HansM






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


Re: redirect standard output problem

2012-12-21 Thread Hans Mulder
On 21/12/12 06:23:18, iMath wrote:
 redirect standard output problem
 
 why the result only print A but leave out 888 ?
 
 import sys
 class RedirectStdoutTo:
 
 def __init__(self, out_new):
 self.out_new = out_new
 def __enter__(self):
 sys.stdout = self.out_new
 def __exit__(self, *args):
 sys.stdout = sys.__stdout__
 
 
 print('A')
 with open('out.log', mode='w', encoding='utf-8') as a_file, 
 RedirectStdoutTo(a_file):
 
 print('B')
 print('C')
 
 print(888)

On my machine it works as you'd expect.

If it doesn't work on your system, it might help to flush
sys.stdout in a few strategic places:

class RedirectStdoutTo:

def __init__(self, out_new):
self.out_new = out_new
def __enter__(self):
sys.stdout.flush()
sys.stdout = self.out_new
def __exit__(self, *args):
sys.stdout.flush()
sys.stdout = sys.__stdout__


print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file,
RedirectStdoutTo(a_file):

print('B')
print('C')

print(888)
sys.stdout.flush()


Hope this helps,

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


Re: Python3 + sqlite3: Where's the bug?

2012-12-20 Thread Hans Mulder
On 20/12/12 16:20:13, Johannes Bauer wrote:
 On 20.12.2012 16:05, Chris Angelico wrote:
 On Fri, Dec 21, 2012 at 1:52 AM, Johannes Bauer dfnsonfsdu...@gmx.de wrote:
 def fetchmanychks(cursor):
 cursor.execute(SELECT id FROM foo;)
 while True:
 result = cursor.fetchmany()
 if len(result) == 0:
 break
 for x in result:
 yield x

 I'm not familiar with sqlite, but from working with other databases,
 I'm wondering if possibly your commits are breaking the fetchmany.

Yes, that's what it looks like.

I think that should be considered a bug in fetchmany.

 Hmm, but this:
 
 def fetchmanychks(cursor):
   cursor.execute(SELECT id FROM foo;)
   while True:
   result = cursor.fetchone()
   if result is not None:
   yield result
   else:
   break
 
 Works nicely -- only the fetchmany() makes the example break.

On my system, fetchmany() defaults to returning only one row.

The documentation says that the default should be the optimal
number of rows per chunk for the underlying database engine.
If the optimum is indeed fetchone one row at a time, then
maybe you could consider using fetchone() as a work-around.

 Would it spoil your performance improvements to do all the
 fetchmany calls before yielding anything?
 
 Well this would effectively then be a fetchall() call -- this is
 problematic since the source data is LARGE (speaking of gigabytes
 of data here).
 
 Alternatively, can you separate the
 two by opening a separate database connection for the foo-reading
 (so it isn't affected by the commit)?
 
 At that point in the code I don't actually have a filename anymore,
 merely the connection. But shouldn't the cursor actually be the
 correct solution? I.e. in theory, should the example work at all
 or am I thinking wrong?

I think you're right and that fetchmany is broken.

 Because if I'm approaching this from the wrong angle, I'll have no
 choice but to change all that code to open separate connections to
 the same file (something that currently are no provisions for).


Hope this helps,

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


Re: Strange effect with import

2012-12-20 Thread Hans Mulder
On 20/12/12 23:52:24, Jens Thoms Toerring wrote:
 I'm writing a TCP server, based on SocketServer:
 
  server = SocketServer.TCPServer((192.168.1.10, 12345), ReqHandler)
 
 where ReqHandler is the name of a class derived from
 SocketServer.BaseRequestHandler
 
  class ReqHandler(SocketServer.BaseRequestHandler):
  ...
 
 A new instance of this class is gernerated for each connection
 request to the server. In the call that creates the server I can
 only specify the name of the class but no arguments to be passed
 to it on instantiation - at least I found nothing in the docu-
 mentation.

What happens if instead of a class you pass a function that
takes the same arguments as the SocketServer.BaseRequestHandler
constructor and returns a new instance of your ReqHandler?

That's not quite what the documentaion clls for, but I'd hope
it's close enough.


Maybe something like this:

class ReqHandler(SocketServer.BaseRequestHandler):
def __init__(self, request, client_address, server, ham, spam)
super(SocketServer, self).__init__(
self, request, client_address, server)
self.ham = ham
self.spam = spam


And later:

import functools

server = SocketServer.TCPServer((192.168.1.10, 12345),
   functools.partial(ReqHandler, ham=hello, spam=42))

 On the other hand I need to get some information into
 this class and thus the only idea I came up with was to use some
 kind of global variable for the purpose. Perhaps there's a much
 better way to do that but I haven't found one yet. Or perhaps it
 is an omission in the design of SocketServer

I think you could call it a weakness in the design of SocketServer.

Life would be easier if it took as an optional third argument some
sequence that it would pass as extra arguments when it instantiates
the handler instance.  Then you wouldn't have to play with functools
(or closures, or global variables) to solve your problems.

 or (more likely) my mis-understanding of the documentation
 (as I wrote I'm relatively new to Python).

From where I sit, it looks like the authors of the SocketServer
module didn't expect subclasses of BaseRequestHandler to need
extra attributes than their base class.

Or maybe they thought everybody knew functools.partial.


Hope this helps,

-- HansM

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


Re: Build and runtime dependencies

2012-12-20 Thread Hans Mulder
On 20/12/12 23:11:45, Jack Silver wrote:
 I have two Linux From Scratch machine.
 
 On the first one (the server), I want to build install python 3.3.0 in a
 shared filesystem and access it from the second one (the client). These
 machines are fairly minimal in term of the number of software installed.
 I just want to install python on this filesystem and anything else.
 
 I would like to know what are the build and runtime dependencies that
 are needed on both machine.
 
 My understanding is that the core CPython interpreter only needs a C
 compiler to be built.

You need the whole C toolchain: compiler, linker, make, etc.

 For the extension modules, I think that only the
 development headers of some additional libraries are needed on the
 server machine. Hence, I do not need to install all those libraries on
 the client machine. Right ?

Wrong.

Those libraries are typically shared libraries (i.e. .so files). You'll
have to install the shared libraries on both the server and the clients.

The development headers are used only at build time, so they are only
needed on the server.

I don't know the package naming conventions on your distro, but on
Debian the packages you only need on the server tend to contain the
word dev.  For example, 'sqlite' would be installed on the client
and bots 'sqlite' and 'sqlite-dev' on the server.

 I would like to build as much module I can, so I have a complete python
 installation. Here is the list of dependecies I think I need to install
 on the server machine :
 
 expat
 bzip2
 gdbm
 openssl
 libffi
 zlib
 tk
 sqlite
 valgrind
 bluez
 
 anything ?

The source comes with a script named configure that tries to find
the headers it needs to build as many extensions modules as possible.

When the script is done, it prints a list of modules it could not
find the headers for.  When this list contains modules you'd like
to build, that means that you're still missing some depencency.

Keep in mind that some modules cannot be built on Linux (for example,
the MacOS module can only be built on MacOS Classic), so you shouldn't
expect to be able to build everything.

 Is there anything I need to install on the client too ?

Yes, the .so files, the actual shared libraries used by these
extensions.


Hope this helps,

-- HansM

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


Re: Problem with Threads

2012-12-19 Thread Hans Mulder
On 19/12/12 18:11:37, Kwnstantinos Euaggelidis wrote:
 I have this code for Prime Numbers and i want to do it with Threads..
 Any idea.??

Why would you want to do that?

It's not going to be any faster, since your code is CPU-bound.
You may have several CPUs, but CPython is going to use only one of them.

If you want to make it faster, read
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

 # prime numbers are only divisible by unity and themselves
 # (1 is not considered a prime number by convention)
 import time
 def isprime(n):
 if n == 2:
 return 1
 if n % 2 == 0:
 return 0
 max = n**0.5+1
 i = 3
 while i = max:
 if n % i == 0:
 return 0
 i+=2
 return 1
 print Please give the maximum number
 endnum = input()
 start = time.time()
 for i in range(endnum):
 if isprime(i) == 1:
 print Number %d is PRIME % i
 print Elapsed Time: %s % (time.time() - start)

Hope this helps,

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


Re: Virtualenv loses context

2012-12-19 Thread Hans Mulder
On 19/12/12 15:38:01, rhythmicde...@gmail.com wrote:
 Just installed a brand new virtualenv along with two packages. Ran this and I 
 got nothing:
 
 (venvtest)[swright@localhost venvtest]$ python -m site
 (venvtest)[swright@localhost venvtest]$ 

 I expected to have at least one path in sys.path

For some reason, that doesn't work with python2.7.
I guess that's a bug in 2.7.

One variant that does work, is:

python -c 'import site; site._script();'

Another would be:

python /usr/lib/python2.7/site.py

If you think you may have accidentally deactivated your
virtualenv, you can do:

python -c 'import sys; print sys.executable;'

Your prompt suggests that venvtest is active.
However, it's possible to deactivate a virtualenv without
resetting the prompt; you may have unintentionally done that.
If that's the problem, you can solve it by sourcing the
activate script again.


Hope this helps,

-- HansM






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


Re: Why does os.stat() tell me that my file-group has no members?

2012-12-19 Thread Hans Mulder
On 19/12/12 22:40:00, saqib.ali...@gmail.com wrote:
 
 
 I'm using python 2.6.4 on Solaris 5-10.
 
 I have a file named myFile. It is owned by someone else, by
 I (myuser) am in the file's group (mygrp). Below is my python
 code. Why does it tell me that mygrp has no members???
 
 
 import os, pwd, grp
 stat_info = os.stat(myFile)
 fileUID = stat_info.st_uid
 fileGID = stat_info.st_gid
 fileGroup = grp.getgrgid(fileGID)[0]
 fileUser = pwd.getpwuid(fileUID)[0]
 print grp.getgrgid(fileGID) = %s % grp.getgrgid(fileGID)
 
 grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', 
 gr_gid=100, gr_mem=[])

It doesn't say that your group has no members.

Every account has a primary group, and some accounts also
have addtional groups.  The primary group is the one in the
.pw_gid attribute in the pwd entry.  The additional groups
are those that mention the account in the .gr_mem attribute
in their grp entry.

Your experiment shows that nobody has mygrp as an additional
group.  So if you're a member of mygrp, then it must be your
primary group, i.e. os.getgid() should return 100 for you.

You can get a complete list of members of group by adding
two lists:

def all_members(gid):
primary_members = [ user.pw_name
for user in pwd.getpwall() if user.pw_gid == gid ]
additional_members = grp.getgrgid(gid).gr_mem
return primary_members + additional_members


Hope this helps,

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 18/12/12 06:10:43, photonym...@gmail.com wrote:
 I hope I understand the question... but shouldn't you wait for the process to 
 complete before exiting?
 
 Something like:
 
 pid = subprocess.Popen(...)
 pid.wait()
 
 Otherwise, it'll exit before the background process is done. 

Why would that be a problem?

-- HansM

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


Re: Delete dict and subdict items of some name

2012-12-18 Thread Hans Mulder
On 18/12/12 06:30:48, Gnarlodious wrote:
 This problem is solved, I am so proud of myself for figuring it out!
 After reading some of these ideas I discovered the plist is really
 lists underneath any Children key:
 
 
 from plistlib import readPlist
 
 def explicate(listDicts):
   for dict in listDicts:
   if 'FavIcon' in dict:
   del dict['FavIcon']
   if 'Children' in dict:
   dict['Children']=explicate(dict['Children'])
   return listDicts

It would be more Pythonic to return None, to indicate that you've
changed the list in situ.

Since None is the default return value, this means you can leave
out the return statement.


Hope this helps,

-- HansM

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 18/12/12 11:39:56, Dave Angel wrote:
 On 12/18/2012 05:27 AM, Hans Mulder wrote:
 On 18/12/12 06:10:43, photonym...@gmail.com wrote:
 I hope I understand the question... but shouldn't you wait for the process 
 to complete before exiting?

 Something like:

 pid = subprocess.Popen(...)
 pid.wait()

 Otherwise, it'll exit before the background process is done. 
 Why would that be a problem?

 
 Because you don't want to bog the system down with a zombie task.

I think you're confusing zombies with orphans.  A zombie results when
a child exits before the parent and the parent fails to wait for it.
An orphan is what you get if the parent exits before the child does.

On a Un*x system, the 'init' process (the process with ID 1) inherits
orphan processes and collects their exit status when they terminate.
Thus orphans do not become zombies.

I have no idea what would happen under Windows.


Hope this helps,

-- HansM

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 17/12/12 21:56:50, py_genetic wrote:
 /usr/local/Calpont/mysql/bin/mysql 
 --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB  
 /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL  /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

If you're trying to interact with a MySQL database, then
you should really use the myslqdb module.  Trying to parse
the output of the command-line utility is harder, and is
too fragile for production use.


Hope this helps,

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


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Hans Mulder
On 18/12/12 22:34:08, Tom Borkin wrote:
 Hi;
 I have this test code:
  
 if i_id == 1186:
   sql = 'insert into interactions values(Null, %s, Call Back,
 %s)' % (i_id, date_plus_2)
   cursor.execute(sql)
   db.commit()
   print sql
 It prints the sql statement, but it doesn't execute. If I copy and paste
 the sql into the mysql command line it does execute without warnings or
 errors. What gives?

What happens if you do:


if i_id == 1186:
  sql = 'insert into interactions values(Null, %s, Call Back, %s)'
  cursor.execute(sql, (i_id, date_plus_2))
  db.commit()
  print sql

Note the absence of quotes around the second %s in the sql command.

This should work correctly even if date_plus_2 happens to contain

 Robert); DROP TABLE interactions; --


For background information, see http://bobby-tables.com/python.html


Hope this helps,

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


Re: Unicode

2012-12-17 Thread Hans Mulder
On 17/12/12 22:09:04, Dave Angel wrote:
 print src.decode(utf-8).encode(latin-1, ignore)
 
 That says to decode it using utf-8 (because the html declared a utf-8
 encoding), and encode it back to latin-1 (because your terminal is stuck
 there), then print.
 
 
 Just realize that once you start using 'ignore' you're going to also
 ignore discrepancies that are real. For example, maybe your terminal is
 actual something other than either latin-1 or utf-8.

If you need to see such discrepancies, you can do

print src.decode(utf-8).encode(latin-1, xmlcharrefreplace)


That would produce something like:

processeurs Intel® Core#8482; de 3ème génération av

that is, the problem characters are displayed in #...; notation.
That is ugly, but sometimes it's the only way to see what character
you really have.

Notice that the number you get is in decimal, where the \u
notation uses hex:

 ord(u\u2122)
8482



Hope this helps,

-- HansM

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


Re: Running a python script under Linux

2012-12-14 Thread Hans Mulder
On 14/12/12 03:45:18, Steven D'Aprano wrote:
 I understand this is not exactly a Python question, but it may be of 
 interest to other Python programmers, so I'm asking it here instead of a 
 more generic Linux group.
 
 I have a Centos system which uses Python 2.4 as the system Python, so I 
 set an alias for my personal use:
 
 [steve@ando ~]$ which python
 alias python='python2.7'
 /usr/local/bin/python2.7
 
 
 When I call python some_script.py from the command line, it runs under 
 Python 2.7 as I expected. So I give the script a hash-bang line:
 
 #!/usr/bin/env python
 
 and run the script directly, but instead of getting Python 2.7, it runs 
 under Python 2.4 and gives me system errors.
 
 When I run env directly, it ignores my alias:
 
 steve@ando ~]$ /usr/bin/env python -V
 Python 2.4.3
 
 
 What am I doing wrong?

You're using an alias.  Aliases are not normally exported, and
even if they are (e.g. ksh can be configure to export aliases),
env doesn't recognize them.

What would work, is changing your PATH environment variable
so that the first python on your PATH is the one you want,
or a symlink pointing to it.

The Pythonic way to get what you want, is to be explicit:

#!/usr/local/bin/python2.7 -V

If you do that, it will even work in situations where you
can't control PATH, such as CGI scripts and cron jobs.

There are situations where using #!/usr/bin/env makes sense,
but yours isn't one of them.

Hope this helps,

-- HansM




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


Re: Running a python script under Linux

2012-12-14 Thread Hans Mulder
On 14/12/12 14:38:25, D'Arcy J.M. Cain wrote:
 On Fri, 14 Dec 2012 14:18:28 +0100
 Hans Mulder han...@xs4all.nl wrote:
 The Pythonic way to get what you want, is to be explicit:

 #!/usr/local/bin/python2.7 -V

 If you do that, it will even work in situations where you
 can't control PATH, such as CGI scripts and cron jobs.
 
 As long as you only run on one system that's OK.

As I understand it, the OP has a single system where the
system Python is CPython 2.4, and he has install 2.7 in
/usr/local/bin.

 That won't work on NetBSD or Linux[1] for example.

I would expect it to work, as long as /usr/local/bin/python2.7
exists and is a binary executable for the right architecture.

Why wouldn't it work?

It doesn't exceed the 32-character limit and it contains
only one option.  What other pitfalls are there?

 There are situations where using #!/usr/bin/env makes sense,
 but yours isn't one of them.
 
 #! /usr/bin/env python2.7

On my box, that line might find a python2.7 in the
currently active virtualenv, which may have the wrong
set of third-party modules in its site-packages.

When I write a script that is meant to be used as a
utility, independent of which virtualenv is currently
active, then I'll make sure that its #! line points
at the Python2.7 install I used to test it.

 [1]: Well, Ubuntu anyway.  I don't know about the others.


Just curious,

-- HansM

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


Re: python 3.3 urllib.request

2012-12-08 Thread Hans Mulder
On 8/12/12 07:20:55, Terry Reedy wrote:
 On 12/7/2012 12:27 PM, Hans Mulder wrote:
 On 7/12/12 13:52:52, Steeve C wrote:
 hello,

 I have a python3 script with urllib.request which have a strange
 behavior, here is the script :

 +

 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-

 import urllib.request
 import sys, time


 url = 'http://google.com'

 def make_some_stuff(page, url):
  sys.stderr.write(time.strftime(%d/%m/%Y %H:%M:%S - page from \)
 + url + \\n)
  sys.stderr.write(str(page) + \\n)
  return True

 def get_page(url):
  while 1:
  try:
  page = urllib.request.urlopen(url)
  yield page

  except urllib.error.URLError as e:
  sys.stderr.write(time.strftime(%d/%m/%Y %H:%M:%S -
 impossible to access to \) + url + \\n)
  time.sleep(5)
  continue

 def main():
  print('in main')
  for page in get_page(url):
  make_some_stuff(page, url)
  time.sleep(5)

 if __name__ == '__main__':
  main()
 +


 if the computer is connected on internet (with an ethernet connection
 for example) and I run this script, it works like a charme :
 - urllib.request.urlopen return the page
 - make_some_stuff write in stderr
 - when the ethernet cable is unplug the except block handle the error
 while the cable is unplug, and when the cable is pluged
 back urllib.request.urlopen return the page and make_some_stuff write in
 stderr

 this is the normal behavior (for me, imho).

 but if the computer is not connected on internet (ethernet cable
 unpluged) and I run this script, the except block handle the error
 (normal), but when I plug the cable, the script continue looping
 and urllib.request.urlopen never return the page (so, it always
 go to the except block)

 What can I do to handle that ?

 Don't do that '-).

 On my laptop, your script works as you'd hope: if I plug in the
 network cable, then the next urllib request sometimes fails, but
 the request after that succeeds.
 This is using Python 3.3 on MacOS X 10.5.
 What version are you running?

 What happens if you start the script with the network cable
 plugged in, then unplug it when the first request has succeeded,
 and then plug it in again when the next request has failed?

 I believe he said that that worked.

You're right: he said that.

 But unplugging cables is not a good idea ;-)

 I remember when it was recommended that all cables be plugged in and the
 the connected devices turned on when the computer was turned on and when
 devices might not be recognized unless plugged in and on when the
 computer was booted or rebooted. In other words, ports were scanned once
 as part of the boot process and adding a device required a reboot.

I also remember the time when that was true.  But these day, many
devices are designed to be plugged in with the computer running,
and the OS continuously scans for new devices.

 It certainly was not that long ago when I had to reboot after the
 Internet Service went down and the cable modem had to reset.

That's a configuration problem: when the cable modem is reset, your
computer needs to rerun its network up script to renew its DHCP lease.
If it isn't configured to do that automatically, and you don't know
how to run it manually, then rebooting may be your only option.

This is a common problem on desktop computers (where losing the
connection to the cable modem is rare).  Laptops are typically
configured to deal with connection appearing and disappearing
on both the wired and the wireless interface.

 Ethernet and usb ports and modern OSes are more forgiving. But it does
 not surprise me if on some systems something has to be presence at
 process startup to even be visible to the process.

His system may be caching the outcome of the IP address lookup.

If that's the case, I'd expect different error messages, depending
on whether the first lookup succeeded or not.  But since his script
carefully avoids printing the exception message, it's hard to tell.

 I believe this is all beyond Python's control. So the only thing to do
 might be to change hardware and/or OS or have the program restart itself
 if it gets repeated errors.

I think that it would deped on the error message.  If the error is
Network is unreachable or No route to host, then sleeping and
trying again might work. If the error is nodename nor servname
provided, or not known, then the script would have to restart itself.


Hope this helps,

-- HansM




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


Re: regex walktrough

2012-12-08 Thread Hans Mulder
On 8/12/12 18:48:13, rh wrote:
  Look through some code I found this and wondered about what it does:
 ^(?Psalsipuedes[0-9A-Za-z-_.//]+)$
 
 Here's my walk through:
 
 1) ^ match at start of string
 2) ?Psalsipuedes if a match is found it will be accessible in a
 variable salsipuedes

I wouldn't call it a variable.  If m is a match-object produced
by this regex, then m.group('salsipuedes') will return the part
that was captured.

I'm not sure, though, why you'd want to define a group that
effectively spans the whole regex.  If there's a match, then
m.group(0) will return the matching substring, and
m.group('salsipuedes') will return the substring that matched
the parenthesized part of the pattern and these two substrings
will be equal, since the only bits of the pattern outside the
parenthesis are zero-width assertions.

 3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see below
 4) + one or more from the preceeding char class
 5) () the grouping we want returned (see #2)
 6) $ end of the string to match against but before any newline
 
 more on #3
 the z-_ part looks wrong and seems that the - should be at the start
 of the char set otherwise we get another range z-_ or does the a-z
 preceeding the z-_ negate the z-_ from becoming a range?

The latter: a-z is a range and block the z-_ from being a range.
Consequently, the -_ bit matches only - and _.

 The . might be ok inside a char set.

It is.  Most special characters lose their special meaning
inside a char set.

 The two slashes look wrong but maybe it has some special meaning
 in some case? I think only one slash is needed.

You're correct: there's no special meaning and only one slash
is needed.  But then, a char set is a set and duplcates are
simply ignored, so it does no harm.

Perhaps the person who wrote this was confusing slashes and
backslashes.

 I've looked at pydoc re, but it's cursory.

That's one way of putting it.


Hope this helps,

-- HansM


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


Re: Issue with seeded map generation

2012-12-08 Thread Hans Mulder
On 8/12/12 22:32:22, Graham Fielding wrote:
 Hey, all!
  
 I've managed to get my project to a semi-playable state (everything
 functions, if not precisely the way I'd like it to).  One small issue is
 that when the player moves from one level to the next, the items and
 monsters in the previous level all 'reset' and return to the positions
 they had when the level was seeded.
 
 I've puzzled over (and attempted) quite a few workarounds, and had no
 success.  I don't want to pickle the entire level (that would be
 overkill for what I need), but I want to update the item/monster
 locations so the player can drop an item and come back to it later.
 
 Should I add something to the 'drop_item' function, or call something
 in make_map?

I think pickling the entire level would be the sensible thing to do.

The alternative would be to keep track of everything that changed
on the level and redo all those changes of the player returns to
the level.  That's a lot of work.  Moreover, everytime you add a
feature to the game, you'd have to extend your keep_track() and
redo() functions, so they can redo the new thing, too.


Hope this helps,

-- HansM


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


Re: regex walktrough

2012-12-08 Thread Hans Mulder
On 8/12/12 23:19:40, rh wrote:
 I reduced the expression too. Now I wonder why re.DEBUG doesn't unroll
 category_word. Some other re flag?

he category word consists of the '_' character and the
characters for which .isalnum() return True.

On my system there are 102158 characters matching '\w':

 sum(1 for i in range(sys.maxunicode+1)
... if re.match(r'\w', chr(i)))
102158


You wouldn't want to see the complete list.

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


Re: regex walktrough

2012-12-08 Thread Hans Mulder
On 8/12/12 23:57:48, rh wrote:
 Not sure if the \w sequence includes the - or the . or the /
 I think it does not.

You guessed right:

 [ c for c in 'x-./y' if re.match(r'\w', c) ]
['x', 'y']


So x and y match \w and  -, . and / do not.


Hope this helps,

-- HansM

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


Re: pyodbc utf-8

2012-12-07 Thread Hans Mulder
On 7/12/12 08:41:27, Markus Christen wrote:
 good morning
 
 i am using pyodbc 3.0.6 for win32 python 2.7.3
 i used it to connect with a MsSql db. Now i have a little problem with the 
 umlaut.
 i cant change anything in the db and there are umlauts like ä, ö
and ü saved.
 so i have to change my view (of django 1.4.1) to change \xfc into ü etc. but 
 how i
 have to do this?

 on my webpage the umlauts are correct (without helping fonts like uuml; (ü)).
 but not the umlauts out read out of my db.

Which encoding does your webpage use?

 Here the code i'm using:
 -
 conn = pyodbc.connect('DRIVER={SQL 
 Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader')
 cursor = conn.cursor()
 cursor.execute(SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama)
 rows = cursor.fetchall()
 -

 helping tags like , 'utf-8' or something else didnt work till now.
 have anyone an idea how i can fix this problem? ^^

I think the way forward would be to look at the data your code snippet
receives from the database.

Which datatype do the strings have?  Unicode or str or something else?

If the type is str, which encoding do they use?
If this isn't documented, you could at a few strings containing
non-ascii characters to see what codes are used, and compare
them to popular encodings such as uft8, latin1 and cp1252.


Hope his helps,

-- HansM


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


Re: python 3.3 urllib.request

2012-12-07 Thread Hans Mulder
On 7/12/12 13:52:52, Steeve C wrote:
 hello,
 
 I have a python3 script with urllib.request which have a strange
 behavior, here is the script :
 
 +
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 import urllib.request
 import sys, time
 
 
 url = 'http://google.com'
 
 def make_some_stuff(page, url):
 sys.stderr.write(time.strftime(%d/%m/%Y %H:%M:%S - page from \)
 + url + \\n)
 sys.stderr.write(str(page) + \\n)
 return True
 
 def get_page(url):
 while 1:
 try:
 page = urllib.request.urlopen(url)
 yield page
 
 except urllib.error.URLError as e:
 sys.stderr.write(time.strftime(%d/%m/%Y %H:%M:%S -
 impossible to access to \) + url + \\n)
 time.sleep(5)
 continue
 
 def main():
 print('in main')
 for page in get_page(url):
 make_some_stuff(page, url)
 time.sleep(5)
 
 if __name__ == '__main__':
 main()
 +
 
 if the computer is connected on internet (with an ethernet connection
 for example) and I run this script, it works like a charme :
 - urllib.request.urlopen return the page
 - make_some_stuff write in stderr
 - when the ethernet cable is unplug the except block handle the error
 while the cable is unplug, and when the cable is pluged
 back urllib.request.urlopen return the page and make_some_stuff write in
 stderr
 
 this is the normal behavior (for me, imho).
 
 but if the computer is not connected on internet (ethernet cable
 unpluged) and I run this script, the except block handle the error
 (normal), but when I plug the cable, the script continue looping
 and urllib.request.urlopen never return the page (so, it always
 go to the except block)
 
 What can I do to handle that ?

On my laptop, your script works as you'd hope: if I plug in the
network cable, then the next urllib request sometimes fails, but
the request after that succeeds.
This is using Python 3.3 on MacOS X 10.5.
What version are you running?

What happens if you start the script with the network cable
plugged in, then unplug it when the first request has succeeded,
and then plug it in again when the next request has failed?

-- HansM



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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

2012-12-06 Thread Hans Mulder
On 6/12/12 11:07:51, iMath wrote:
 the following code originally from 
 http://zetcode.com/databases/mysqlpythontutorial/
 within the Writing images part .
 
 
 import MySQLdb as mdb
 import sys
 
 try:
 fin = open(Chrome_Logo.svg.png,'rb')
 img = fin.read()
 fin.close()
 
 except IOError as e:
 
 print (Error %d: %s % (e.args[0],e.args[1]))
 sys.exit(1)
 
 
 try:
 conn = mdb.connect(host='localhost',user='testuser',
passwd='test623', db='testdb')
 cursor = conn.cursor()
 cursor.execute(INSERT INTO Images SET Data='%s' % \
 mdb.escape_string(img))

You shouldn't call mdb.escape_string directly.  Instead, you
should put placeholders in your SQL statement and let MySQLdb
figure out how to properly escape whatever needs escaping.

Somewhat confusingly, placeholders are written as %s in MySQLdb.
They differ from strings in not being enclosed in quotes.
The other difference is that you'd provide two arguments to
cursor.execute; the second of these is a tuple; in this case
a tuple with only one element:

cursor.execute(INSERT INTO Images SET Data=%s, (img,))

 conn.commit()
 
 cursor.close()
 conn.close()
 
 except mdb.Error as e:
 
 print (Error %d: %s % (e.args[0],e.args[1]))
 sys.exit(1)
 
 
 I port it to python 3 ,and also change 
 fin = open(chrome.png) 
 to 
 fin = open(Chrome_Logo.png,'rb')
 but when I run it ,it gives the following error :
 
 Traceback (most recent call last): 
   File E:\Python\py32\itest4.py, line 20, in module
 mdb.escape_string(img))
 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: 
 invalid start byte
 
 so how to fix it ?

Python 3 distinguishes between binary data and Unicode text.
Trying to apply string functions to images or other binary
data won't work.

Maybe correcting this bytes/strings confusion and porting
to Python 3 in one go is too large a transformation.  In
that case, your best bet would be to go back to Python 2
and fix all the bytes/string confusion there.  When you've
got it working again, you may be ready to port to Python 3.


Hope this helps,

-- HansM

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


Re: Confused compare function :)

2012-12-06 Thread Hans Mulder
On 6/12/12 12:55:16, peter wrote:
 Is perfectly right to use try catch for a flow control.
 Just think in something more complex like this.
 
try:
 self._conn = MySQLdb.connect(host=host,
 user=user,
 passwd=passwd,
 db=db)
 except:
 logging.info(Error de conexion con la base de datos)
 inform(subject = 'Db down on app %s' % app, body=sbody)

This is an example of the sort of incorrect code you
should try to avoid.  An improved version is:

   self._conn = MySQLdb.connect(host=host,
user=user,
passwd=passwd,
db=db)

By not catching the exception, you're allowing the
Python interpreter to report what the problem was,
for example Keyboard interrupt or Access denied.

By report DB down when there is no reason to assume
that that is the problem, you're confusing the user.

 Or maybe something like this.
 
 try:
 cursor.execute(sqli, data)
 self._conn.commit()
 except:
 try:
 self._conn.rollback()
 cursor.execute(sqli, data)
 self._conn.commit()
 except Exception, e:
 pass
 # print e
 # logging.info('ERROR en la insercion %s' % e)

This is another example of what not to do.  Even the
commented-out print statement loses information, viz.
the traceback.

If you leave out the try/except, then more accurate
information will be printed, and a programmer who needs
to fix the problem, can run the code under the debugger
and it will automatically stop at the point where the
uncaught exception is raised.  That's much easier than
having to set breakpoints at all the except Exception:
clauses in a typical chunk of hard-to-maintain code.

Context managers were invented to make it easier to do
this sort of thing correctly.  For example:

with sqlite3.connect(dbpath) as connection:
connection.cursor().execute(sqli, data)

If the flow reaches the end of the with command,
the connection object will self.commit() automatically.
If an exception is raised, the connection object will
self.rollback() automatically.  No try/except required.

This is shorter, and much easier to get right.

 This is pretty dumb, but is a valid example, on what you can
 do with try catch

It is an unfortunate fact of life that you can write code
that is hard to maintain.  The fact that you *can* do this,
does not mean that you should.


Hope this helps,

-- HansM

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


Re: Confused compare function :)

2012-12-06 Thread Hans Mulder
On 6/12/12 14:58:01, Chris Angelico wrote:
 On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel
 nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa...@spamschutz.glglgl.de
 wrote:
  Am 06.12.2012 09:49 schrieb Bruno Dupuis:
 
  The point is Exceptions are made for error handling, not for normal
  workflow. I hate when i read that for example:
 
   try:
   do_stuff(mydict[k])
   except KeyError:
   pass
 
  I would do
 
  try:
  value = mydict[k]
  except KeyError:
  pass
  else:
  do_stuff(k)
 
  Why? Because do_stuff() might raise a KeyError, which should not go
  undetected.

 (Assuming first off that you meant do_stuff(value), not
 do_stuff(k), in that last line)

 That has quite different functionality, though. The original wouldn't
 have called do_stuff at all if k is not in dict, behaviour which is
 matched by both his EAFP and his LBLY. But your version, in the event
 of a KeyError, will call do_stuff with the previous value of value, or
 raise NameError if there is no such previous value. I don't think
 that's intentional.

Errhm, no.  Look again.  The do_stuff(value) call is in the else:
clause, so it will only be done of there was no Exception of any
kind, and in that case the assignment to value must have succeeded.

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


Re: mini browser with python

2012-12-05 Thread Hans Mulder
On 5/12/12 20:36:04, inq1ltd wrote:
 Python help.

?This is not a Python question.

 I can connect to and download a web page,
 html code, and save it to a file. If connected
 to the web, I can use KWrite to open the file
 and navigate the page.

 I want to view the html file without using a browser
 or KWrite as I do now.

 In other words I need a mini, simple browser;
 something I can build that will open, read and
 display a saved html file.

Why don't you use the browser you already know?

 I would appreciate some direction.

You're more likely to get good recommendations on
a forum dedicated to whichever OS you are using.



Hope this help,

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


Re: mini browser with python

2012-12-05 Thread Hans Mulder
On 5/12/12 22:44:21, inq1ltd wrote:

 I can connect to and download a web page,
 html code, and save it to a file. If connected
 to the web, I can change the settings on KWrite
 to open the file and navigate the page,
 (just like a browser does).

 I want to view the html file without using a browser

 or KWrite as I do now.

 Customer wants a direct connect to a dedicated
 website. Their clients can be using any
 browser. This gives them a a direct connect to
 a dedicated website for a specific purpose.
 In other words I need a mini, simple browser;
 something I can build that will open, read and
 display a saved html or the connected url site.

How about:

import os.path, webbrowser

webbrowser.open(file:// + os.path.abspath(your_file))

 I would appreciate some direction.

You'd get more useful answers if you'd give some
more context.  For example:

* Which platform?
* Which version of Python?
* Which GUI (if any)?
* What capabilities do you need?  CSS? Javascript?
* Are there any unusual requirement?


Hope this helps,

-- HansM


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


Re: Installing packages on Mac OS X 10.7.5

2012-12-05 Thread Hans Mulder
On 6/12/12 00:56:55, Irmen de Jong wrote:
 On 6-12-2012 0:12, John Dildy wrote:
 I have python v2.7.1 and I am trying to install packages on the Mac OS X 
 v10.7.5
 I am trying to install:
 Distribute
 Nose
 virtualenv
 If anyone can help me that would be great
 
 Avoid changing stuff on the system installed python.

+1

 If you don't have virtualenv already, I would suggest to either:
 
 - install virtualenv by means of easy_install (which should be installed 
 already)
 - do everything else in a virtual env, instead of in the system installed 
 python directly
 
 Or install homebrew, then brew install python, and use that. This avoids 
 using the
 system installed python entirely.

Or simply download Python 2.7.3 from python.org and install that.

The Mac OSX installers from python.org leave the system Python
alone and install a separate copy of Python under /Local/.

Or you could use MacPorts, which will install into /opt/local/.

Or you could get really fancy, and install a Python from python.org
or homebrew or MacPorts, and then user virtualenv to avoid messing up
that Python.

The one thing you want to avoid is messing with the system Python.


Hope this helps,

-- HansM

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


Re: Conversion of List of Tuples

2012-12-04 Thread Hans Mulder
On 4/12/12 10:44:32, Alexander Blinne wrote:
 Am 03.12.2012 20:58, schrieb subhabangal...@gmail.com:
 Dear Group,

 I have a tuple of list as,

 tup_list=[(1,2), (3,4)]
 Now if I want to covert as a simple list,

 list=[1,2,3,4]

 how may I do that?
 
 Another approach that has not yet been mentioned here:
 
 a=[(1,2), (3,4)]
 b=[]
 map(b.extend, a)
 [None, None]
 b
 [1, 2, 3, 4]
 
 map returns [None, None] because extend returns nothing, but now
 b==[1,2,3,4].

It's considered bad style to use map it you don't want the list it
produces.

 There are more ways:
 
 from operator import add
 reduce(add, a)
 (1, 2, 3, 4)

There's a built-in that does reduce(operator.add; it's called sum:

 sum(a, ())
(1, 2, 3, 4)

 or
 
 reduce(operator.add, (list(t) for t in a))
 [1, 2, 3, 4]

This is a valid use case for the map operator:

 sum(map(list, a), [])
[1, 2, 3, 4]


 I didn't do any performance testing, i guess the first one should be
 about as fast as the for-loop approach with .extend() and the other two
 might be quite slow. Although this only really matters if you have large
 lists.

Hope this helps,

-- HansM



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


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Hans Mulder
On 2/12/12 18:25:22, Roy Smith wrote:
 This is kind of weird (Python 2.7.3):
 
 try:
 print hello
 except foo:
 print foo
 
 prints hello.  The problem (IMHO) is that apparently the except clause 
 doesn't get evaluated until after some exception is caught.  Which means 
 it never notices that foo is not defined until it's too late.
 
 This just came up in some code, where I was trying to catch a very rare 
 exception.  When the exception finally happened, I discovered that I had 
 a typo in the except clause (I had mis-spelled the name of the 
 exception).  So, instead of getting some useful information, I got an 
 AttributeError :-(
 
 Is this a bug, or intended behavior?  It seems to me it would be much 
 more useful (if slightly more expensive) to evaluate the names of the 
 exceptions in the expect clause before running the try block.

It's intended behaviour: Python runs your script from top to bottom.
It doesn't peek ahead at except: clauses.  Even it an exception is
raised, the exception names are not eveluated all at once.  Python
begins by evaluating the first exception name.  It the exception at
hand is an instance of that, Python has found a match and the rest
of the names are left unevaluated:

 try:
...  1/0
... except ZeroDivisionError:
...  print hello
... except 1/0:
...  pass
...
hello

There's probably some lint-like tool that can find this kind of issue.


Hope this helps,

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


Re: Weird import failure with nosetests --processes=1

2012-11-29 Thread Hans Mulder
On 29/11/12 04:13:57, Roy Smith wrote:
 I've got a minimal test script:
 
 -
 $ cat test_foo.py
 import pyza.models
 print pyza.models
 
 def test_foo():
 pass
 -
 
 pyza.models is a package.  Under normal conditions, I can import it fine:
 
 $ python
 Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
 [GCC 4.6.3] on linux2
 Type help, copyright, credits or license for more information.
 import pyza.models
 print pyza.models
 module 'pyza.models' from 
 '/home/roy/deploy/current/pyza/models/__init__.pyc'

 
 But when I run nosetests in parallel mode, the import fails in a way 
 which has me baffled.  What's going on here?
 
 $ nosetests --processes=1 -s test_foo:test_foo
 module 'pyza.models' from 
 '/home/roy/deploy/current/pyza/models/__init__.pyc'
 E
 ==
 ERROR: Failure: AttributeError ('module' object has no attribute 
 'models')
 --
 Traceback (most recent call last):
   File 
 /home/roy/production/python/local/lib/python2.7/site-packages/nose/loade
 r.py, line 390, in loadTestsFromName
 addr.filename, addr.module)
   File 
 /home/roy/production/python/local/lib/python2.7/site-packages/nose/impor
 ter.py, line 39, in importFromPath
 return self.importFromDir(dir_path, fqname)
   File 
 /home/roy/production/python/local/lib/python2.7/site-packages/nose/impor
 ter.py, line 86, in importFromDir
 mod = load_module(part_fqname, fh, filename, desc)
   File /home/roy/songza/pyza/djapi/test_foo.py, line 2, in module
 print pyza.models
 AttributeError: 'module' object has no attribute 'models'
 
 --
 Ran 1 test in 0.107s
 
 FAILED (errors=1)


That is baffling indeed.  It looks like nose is adding some
directory to sys.path, which contains a module pyza.py instead
of a package.

One thing you could try, is changing line 2 of you script to just

print pyza

, to see if pyza is being loaded from
/home/roy/deploy/current/pyza/__init__.pyc, as you'd expect.  If it
isn't, you'll be
one step closer to a solution.

Another idea might be to delete all *.pyc files below
/home/roy/deploy/current/ and /home/roy/songza/pyza/djapi/.
That would help if a .pyc file is somehow out of sync with
the corresponding .py file.  That's not supposed to happen,
but if it does, you can get weird results.

One last idea: put these lines at the top of test_foo.py

import pdb
pdb.set_trace()

Running under nosetest should then drop you in the debugger.
Single step into the next statement (import pyza.models) to
see where this gets you.  It should import pyza, and then
pyza.models.  Add some print statements to the __init__.py
files of those packages, so that there's somewhere for pdb
to stop and prompt.


Hope this helps,

-- HansM








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


Re: How to pass class instance to a method?

2012-11-26 Thread Hans Mulder
On 27/11/12 00:07:10, Ian Kelly wrote:
 On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel d...@davea.name wrote:
 Not how I would put it.  In a statically typed language, the valid types
 are directly implied by the function parameter declarations,
 
 As alluded to in my previous post, not all statically typed languages
 require parameter type declarations to perform static checking.
 
 while in a
 dynamic language, they're defined in the documentation, and only
 enforced (if at all) by the body of the function.
 
 That's not even true for Python.  The following example uses Python 2.x:
 
 class Foo(object):
 ... def method(self):
 ... pass
 ...
 Foo.method(4)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: unbound method method() must be called with Foo instance as
 first argument (got int instance instead)
 
 That's a run-time check, and it's not enforced by the body of the function.

As Ian already knows, this problem has been fixed in Python 3.

-- HansM

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


Re: Getting a seeded value from a list

2012-11-26 Thread Hans Mulder
On 26/11/12 21:17:40, Prasad, Ramit wrote:
 Chris Angelico wrote:

 On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit
 ramit.pra...@jpmorgan.com wrote:
 Steven D'Aprano wrote:

 On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote:

 However, this still means that the player will see the exact same level
 regenerated every time, absolutely fresh. As previously stated in this
 thread, that's not usually a good thing for encounters, treasure, etc.
 Once some nasty critter has been killed, he should STAY killed! :)

 Why? That isn't true in real life, why should it be true for games?


 It is not true in all games. I have seen games where treasures
 regenerate in the same location except for key items. Same goes
 for enemies (where only bosses do not regenerate). It really
 just depends on the type of game you are playing--designing
 in this case.

 Perhaps they regenerate, but do they regenerate from the exact same
 random seed? For instance, in Murkon's Refuge, the maps are
 handcrafted and thus constant every time you enter a particular level
 - but go downstairs and upstairs, and the monsters and treasure
 regenerate, different from last time.

 Of course, if the idea is that you're rewinding time, then it makes
 good sense for you to see the exact same pattern of enemies.

 
 Hmm. I guess most of the games where I remember regenerating
 enemies are a bit older. Chrono Trigger had enemies that 
 would regenerate if you left a map screen and came back, but
 then again it seems more likely that the enemies were hard
 coded and not generated at all. Either that or they [games] are
 like Diablo/Castlevania where monsters are constantly generated.
 
 I still hold the opinion that the seeding behavior depends on the 
 game. I wonder what Nethack does?

When you leave a level, Nethack saves it to the hard disk.
When you return, the level is reloaded from disk, so monsters
and objects appear where they were when you left the level.
The map is also saved, so if you had dug an extra corridor
on your first visit, it'll be there when you return.

The random level generation only happens if you go to a level
you haven't visited before in that game (and sometimes not
even then).

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


Re: How to get a screen length of a multibyte string?

2012-11-25 Thread Hans Mulder
On 25/11/12 11:19:18, kobayashi wrote:
 Hello,
 
 Under platform that has fixed pitch font,
 I want to get a screen length of a multibyte string
 
 --- sample ---
 s1 = uabcdef
 s2 = uあいう # It has same screen length as s1's.
 print len(s1)  # Got 6
 print len(s2)  # Got 3, but I want get 6.
 --
 
 Abobe can get a character length of a multibyte string.
 Is there a way to get a screen length of a multibyte string?

How about:

from unicodedata import east_asian_width

def screen_length(s):
return sum(2 if east_asian_width(c) == 'W' else 1 for c in s)


Hope this helps,

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


Re: Print value from array

2012-11-23 Thread Hans Mulder
On 22/11/12 19:44:02, Mike wrote:
 Hello,
 I am noob en python programing,  i wrote a perl script for read from csv but 
 now i wish print value but the value must be within double quote and I can 
 not do this. 
 
 For example now the output is:
 
 ma user@domain displayName Name SecondName givenName Name sn SecondName cn 
 Name
 
 and i wish
 
 ma user@domain displayName Name Lastname givenName Name sn SecondName 
 cn Name
 
 My script is
 
 #!/usr/bin/python
 import csv
 
 with open ('file.csv', 'rb') as f:
   reader = csv.reader (f, delimiter=';' )
   for row in reader:
   mail = row [0]
   name = row [1]
   lastname = row [2]
   name2  = row [1] + ' ' + row [2]
   
   print  'ma ' + mail + ' displayName ' +  name2.title() + ' 
 givenName ' + name.title() + ' sn ' + lastname.title() + ' cn ' + 
 name.title()  
 
   #   print '\n'
 
 f.close() 

How about:

#!/usr/bin/python
import csv

with open('file.csv', 'rb') as f:
reader = csv.reader(f, delimiter=';' )
for mail, firstname, lastname in reader:
fullname  = firstname + ' ' + lastname

print 'ma %s' % mail,
print 'displayname %s' % fullname.title(),
print 'givenName %s' % firstname.title(),
print 'sn %s' % lastname.title(),
print 'cn %s' % firstname.title()

f.close()


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


Re: 10 sec poll - please reply!

2012-11-21 Thread Hans Mulder
On 21/11/12 02:17:26, Steven D'Aprano wrote:
 On Tue, 20 Nov 2012 18:00:59 -0600, Tim Chase wrote:
 
 On 11/20/12 06:18, Michael Herrmann wrote:
 am having difficulty picking a name for the function that simulates key
 strokes. I currently have it as 'type' but that clashes with the
 built-in function.

 Just to add one more to the pot, Vim uses feedkeys() for a similar
 purpose.
 
 What does it feed to the keys?

Spam, spam, spam, eggs, and spam.

-- HansM


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


Re: Inconsistent behaviour os str.find/str.index when providing optional parameters

2012-11-21 Thread Hans Mulder
On 21/11/12 17:59:05, Alister wrote:
 On Wed, 21 Nov 2012 04:43:57 -0800, Giacomo Alzetta wrote:
 
 I just came across this:

 'spam'.find('', 5)
 -1


 Now, reading find's documentation:

 print(str.find.__doc__)
 S.find(sub [,start [,end]]) - int

 Return the lowest index in S where substring sub is found,
 such that sub is contained within S[start:end].  Optional arguments
 start and end are interpreted as in slice notation.

 Return -1 on failure.

 Now, the empty string is a substring of every string so how can find
 fail?
 find, from the doc, should be generally be equivalent to
 S[start:end].find(substring) + start, except if the substring is not
 found but since the empty string is a substring of the empty string it
 should never fail.

 Looking at the source code for find(in stringlib/find.h):

 Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t offset)
 {
 Py_ssize_t pos;

 if (str_len  0)
 return -1;

 I believe it should be:

 if (str_len  0)
 return (sub_len == 0 ? 0 : -1);

 Is there any reason of having this unexpected behaviour or was this
 simply overlooked?
 
 why would you be searching for an empty string?
 what result would you expect to get from such a search?


In general, if

needle in haystack[ start: ]

return True, then you' expect

haystack.find(needle, start)

to return the smallest i = start such that

haystack[i:i+len(needle)] == needle

also returns True.

  in spam[5:]
True
 spam[5:5+len()] == 
True


So, you'd expect that spam.find(, 5) would return 5.

The only other consistent position would be that spam[5:]
should raise an IndexError, because 5 is an invalid index.

For that matter, I wouldn;t mind if spam.find(s, 5) were
to raise an IndexError.  But if slicing at position 5
proudces an empry string, then .find should be able to
find that empty string.

-- HansM

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


Re: mysql insert with tuple

2012-11-21 Thread Hans Mulder
On 21/11/12 18:19:15, Christian wrote:
 Hi ,
 
 my purpose is a generic insert via  tuple , because the number of fields and 
 can differ. But  I'm stucking .
 
 ilist=['hello',None,7,None,None]
 
 #This version works, but all varchar fields are in extra '' enclosed.
 con.execute( INSERT INTO {} VALUES %r; .format(table) , (tuple(ilist),))
 
 #This produce (1054, Unknown column 'None' in 'field list'),
 #but without None values it works.
 con.execute( INSERT INTO {} VALUES %r; .format(table) % (tuple(ilist),))

How about:

con.execute(INSERT INTO {} VALUES ({})
.format(table, ,.join(%s for _ in ilist)), ilist)

Or perhaps break it down into smaller steps:

bind_variables = ,.join(%s for _ in ilist))
query = INSERT INTO {} VALUES ({}).format(table, bind_variables)
con.execute(query, ilist)


Hope this helps,

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


Re: Premature end of script headers: wsgihandler.py on usage of BytesIO()

2012-11-19 Thread Hans Mulder
On 19/11/12 14:29:13, Yasir Saleem wrote:
 Hi all,
 
 I am generating graphs using cairo plot in web2py project.
 Here I am using BytesIO() stream for generating graphs.
 Everything runs fine when I run on localhost but when I deploy
 it on apache server and then run from different machines OR
 from different browsers in same machine then the server
 becomes halt and in apache error log, I found this error
 message:

 Premature end of script headers: wsgihandler.py,
 
 Furthermore, it occurs only when I use BytesIO() stream for
 generating graphs. In all other cases, it run smoothly.
 
 Please guide me that how I should resolve this issue.

One technique is to put at the very top of the script, even
above the import statements:

print Content-Type: text/plain\n\n

This will cause all text output by your script to be displayed
in your browser as plain text [1].  If you inspect it, you'll
probably find some kind of warning displayed above the HTML
headers.  You'll need to find a way to not receive that warning.

It's usually best if you can actually solve the issue Python is
warning about.  it that's not possible, suppressing the warning
may be your only alternative.

If you can't figure out what the message means, and Google
doesn't know either, you can post it in this forum and ask
for further guidance.

[1] Except if you use Internet Explorer, which will ask you
whether you want to save the document.  You can either do that
and view the content with another application, or use another
browser, or change the content-type to text/html.  If you do
the latter, IE will notice that the content is really plain
text, and that it is actually quite capable of displaying that.


Hope this helps,

-- HansM

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


Re: Unpaking Tuple

2012-11-18 Thread Hans Mulder
On 9/10/12 08:07:32, Bob Martin wrote:
 in 682592 20121008 232126 Prasad, Ramit ramit.pra...@jpmorgan.com wrote:
 Thomas Bach wrote:=0D=0A Hi there,=0D=0A =0D=0A On Sat, Oct 06, 2012 at =
 03:08:38PM +, Steven D'Aprano wrote:=0D=0A =0D=0A  my_tuple =3D my_=
 tuple[:4]=0D=0A  a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
 tuple + (None,)*4)[:4]=0D=0A =0D=0A =0D=0A Are you sure this works as y=
 ou expect? I just stumbled over the following:=0D=0A =0D=0A $ python=0D=
 =0A Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A [GCC 4=2E7=2E=
 1] on linux2=0D=0A Type help, copyright, credits or license for mo=
 re information=2E=0D=0A  split =3D ['foo', 'bar']=0D=0A  head, tail=
 =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A  head=0D=0A=
 ['foo', 'bar']=0D=0A  tail=0D=0A =0D=0A =0D=0A I don't get it! =
 Could someone help me, please? Why is head not 'foo'=0D=0A and tail not 'b=
 ar'?=0D=0A =0D=0A Regards,=0D=0A  Thomas=0D=0A --=0D=0A=0D=0AI think yo=
 u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
 ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
 e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
 else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
 n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
 mail is confidential and subject to important disclaimers and=0D=0Aconditio=
 ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
 y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
 vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
 an=2Ecom/pages/disclosures/email=2E
 
 How does one unpack this post?  ;-)

How about:

print re.sub('^* ', '', this_post, flags=re.M).decode('quopri')


Hope this helps,

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-14 Thread Hans Mulder
On 13/11/12 22:36:47, Thomas Rachel wrote:
 Am 12.11.2012 19:30 schrieb Hans Mulder:
 
 This will break if there are spaces in the file name, or other
 characters meaningful to the shell.  If you change if to

  xargsproc.append(test -f '%s/{}'  md5sum '%s/{}'
   % (mydir, mydir))

 , then it will only break if there are single quotes in the file name.
 
 And if you do mydir_q = mydir.replace(', '\\'') and use mydir_q, you
 should be safe...

The problem isn't single quotes in mydir, but single quotes in the
files names that 'tar' generates and 'xargs' consumes.  In the shell
script, these names go directly from tar to xargs via a pipe.  If the
OP wants to do your replace, his script would have to read the output
of tar and do the replace before passing the filenames down a second
pipe to xargs.

However, once he does that, it's simpler to cut out xargs and invoke
sh directly.  Or even cut out sh and test and instead use
os.path.isfile and then call md5sum directly.  And once he does that,
he no longer needs to worry about single quotes.

The OP has said, he's going to d all that.  One step at a time.
That sounds like a sensible plan to me.


Hope this helps,

-- HansM


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


Re: Detect file is locked - windows

2012-11-14 Thread Hans Mulder
On 14/11/12 02:14:59, Mark Lawrence wrote:
 On 14/11/2012 00:33, Ali Akhavan wrote:
 I am trying to open a file in 'w' mode open('file', 'wb'). open() will
 throw with IOError with errno 13 if the file is locked by another
 application or if user does not have permission to open/write to the
 file.

 How can I distinguish these two cases ? Namely, if some application
 has the file open or not.

I don't have a Windows machine at hand to try, but this might work:

if exc.errno == 13:
if os.access('file', os.W_OK):
print Locked by another process
else:
print No permission to write

 Anything here help http://www.python.org/dev/peps/pep-3151/ ?

That won't help: in Python 3.3, IOError with errno==13 has been
replaced by PermissionError.  It still doesn't tell you *why*
you got a PermissionError.


Hope this helps,

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


Re: Detect file is locked - windows

2012-11-14 Thread Hans Mulder
On 14/11/12 11:02:45, Tim Golden wrote:
 On 14/11/2012 00:33, Ali Akhavan wrote:
 I am trying to open a file in 'w' mode open('file', 'wb'). open()
 will throw with IOError with errno 13 if the file is locked by
 another application or if user does not have permission to open/write
 to the file.
 
 What version of Python are you using?
 

 How can I distinguish these two cases ? Namely, if some application
 has the file open or not.
 
 Can I ask what you expect to do differently in each of those cases? In
 other words, if you can't access the file, you can't access it. (Not to
 dismiss your question; I just wonder how you're going to handle the
 different cases)

It would be nice if he could give specific error messages, e.g.

Can't write %s because it is locked by %s.

vs.

Can't write %s because you don't have write access.

I can't speak for Ali, but I'm always annoyed by error messages
listing several possible cuases, such as Can't delete file,
because the source or destination is in use.

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread Hans Mulder
On 12/11/12 16:36:58, jkn wrote:
 slight followup ...
 
 I have made some progress; for now I'm using subprocess.communicate to
 read the output from the first subprocess, then writing it into the
 secodn subprocess. This way I at least get to see what is
 happening ...
 
 The reason 'we' weren't seeing any output from the second call (the
 'xargs') is that as mentioned I had simplified this. The actual shell
 command was more like (in python-speak):
 
 xargs -I {} sh -c \test -f %s/{}  md5sum %s/{}\ % (mydir, mydir)
 
 ie. I am running md5sum on each tar-file entry which passes the 'is
 this a file' test.
 
 My next problem; how to translate the command-string clause
 
 test -f %s/{}  md5sum %s/{} # ...
 
 into s parameter to subprocss.Popen(). I think it's the command
 chaining '' which is tripping me up...

It is not really necessary to translate the '': you can
just write:

test -f '%s/{}'  md5sum '%s/{}' % (mydir, mydir)

, and xargs will pass that to the shell, and then the shell
will interpret the '' for you: you have shell=False in your
subprocess.Popen call, but the arguments to xargs are -I {}
sh -c , and this means that xargs ends up invoking the
shell (after replacing the {} with the name of a file).

Alternatively, you could translate it as:

if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi % (mydir, mydir)

; that might make the intent clearer to whoever gets to
maintain your code.


Hope this helps,

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread Hans Mulder
On 12/11/12 18:22:44, jkn wrote:
 Hi Hans
 
 On Nov 12, 4:36 pm, Hans Mulder han...@xs4all.nl wrote:
 On 12/11/12 16:36:58, jkn wrote:









 slight followup ...

 I have made some progress; for now I'm using subprocess.communicate to
 read the output from the first subprocess, then writing it into the
 secodn subprocess. This way I at least get to see what is
 happening ...

 The reason 'we' weren't seeing any output from the second call (the
 'xargs') is that as mentioned I had simplified this. The actual shell
 command was more like (in python-speak):

 xargs -I {} sh -c \test -f %s/{}  md5sum %s/{}\ % (mydir, mydir)

 ie. I am running md5sum on each tar-file entry which passes the 'is
 this a file' test.

 My next problem; how to translate the command-string clause

 test -f %s/{}  md5sum %s/{} # ...

 into s parameter to subprocss.Popen(). I think it's the command
 chaining '' which is tripping me up...

 It is not really necessary to translate the '': you can
 just write:

 test -f '%s/{}'  md5sum '%s/{}' % (mydir, mydir)

 , and xargs will pass that to the shell, and then the shell
 will interpret the '' for you: you have shell=False in your
 subprocess.Popen call, but the arguments to xargs are -I {}
 sh -c , and this means that xargs ends up invoking the
 shell (after replacing the {} with the name of a file).

 Alternatively, you could translate it as:

 if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi % (mydir, mydir)

 ; that might make the intent clearer to whoever gets to
 maintain your code.
 
 Yes to both points; turns out that my problem was in building up the
 command sequence to subprocess.Popen() - when to use, and not use,
 quotes etc. It has ended up as (spelled out in longhand...)
 
 
 xargsproc = ['xargs']
 
 xargsproc.append('-I')
 xargsproc.append({})
 
 xargsproc.append('sh')
 xargsproc.append('-c')
 
 xargsproc.append(test -f %s/{}  md5sum %s/{} % (mydir,
 mydir))

This will break if there are spaces in the file name, or other
characters meaningful to the shell.  If you change if to

xargsproc.append(test -f '%s/{}'  md5sum '%s/{}'
 % (mydir, mydir))

, then it will only break if there are single quotes in the file name.

As I understand, your plan is to rewrite this bit in pure Python, to
get rid of any and all such problems.

 As usual, breaking it all down for the purposes of clarification has
 helpd a lot, as has your input. Thanks a lot.

You're welcome.

-- HansM


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


[issue16450] test_missing_localfile masks problems in urlopen

2012-11-10 Thread Hans Mulder

New submission from Hans Mulder:

Due to a misconfiguration, urllib.thishost() raises an IOError
on my laptop.  This causes urllib.urlopen to raise an exception.
A flaw in test_missing_localfile causes this exception to not be
reported.  The problem happens at line 230-235:

try:
self.assertTrue(os.path.exists(tmp_file))
fp = urllib.urlopen(tmp_fileurl)
finally:
os.close(fd)
fp.close()

On my laptop, urllib.urlopen raises a socket.gaierror.  This
means that the variable fp is never set, and the fp.close() at
line 235 raises an UnboundLoccalError, masking the socket error.

A quick fix would be:

try:
self.assertTrue(os.path.exists(tmp_file))
fp = urllib.urlopen(tmp_fileurl)
fp.close()
finally:
os.close(fd)

That way, the .close is only attempted if the open succeeds.

--
components: Tests
messages: 175278
nosy: HansM
priority: normal
severity: normal
status: open
title: test_missing_localfile masks problems in urlopen
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16450
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Obnoxious postings from Google Groups

2012-11-09 Thread Hans Mulder
On 6/11/12 23:50:59, Steven D'Aprano wrote:
 On Tue, 06 Nov 2012 17:16:44 +, Prasad, Ramit wrote:
 
 To enter the newline, I typed Ctrl-Q to tell bash to treat the next
 character as a literal, and then typed Ctrl-J to get a newline.

 That sounds complicated, my version of bash lets me type
 'fooenterbar'enter for the same effect.
 
 Well, I learned something new about bash.
 
 On the other hand, the Ctrl-Q next-char-is-literal trick works for 
 entering control characters that otherwise don't have a key on the 
 keyboard.

How does that trick work?  If I need a control character
that is not available in my current keyboard mapping, how
would I enter such a character using this Ctrl-Q trick?


Just wondering,

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


Re: Obnoxious postings from Google Groups

2012-11-09 Thread Hans Mulder
On 7/11/12 01:13:47, Steven D'Aprano wrote:
 On Tue, 06 Nov 2012 23:08:11 +, Prasad, Ramit wrote:
 
 Steven D'Aprano wrote:

 On Tue, 06 Nov 2012 17:16:44 +, Prasad, Ramit wrote:

 To enter the newline, I typed Ctrl-Q to tell bash to treat the next
 character as a literal, and then typed Ctrl-J to get a newline.

 That sounds complicated, my version of bash lets me type
 'fooenterbar'enter for the same effect.

 Well, I learned something new about bash.

 On the other hand, the Ctrl-Q next-char-is-literal trick works for
 entering control characters that otherwise don't have a key on the
 keyboard.


 Would you mind elaborating on how this works? I know it's not a bash
 list, but I do not understand how ctrl-J is considered a literal.
 Obviously, I must have a different definition of literal. Where can I
 find a list of other literals? My Google-fu is being weak today. :(
 
 I'm not an expert, so the following may not be exactly correct. As I 
 understand it, when you hit a key on the keyboard, it sends the character 
 you typed to the operating system. (The OS can then remap keys, generate 
 keyboard events including a timestamp, etc.)
 
 Hit the J key, and the event includes character j. Hit Shift-J, and 
 character J is sent. Hit Ctrl-J, and the character sent is the ASCII 
 control character ^J, or newline. (Technically, the name for ASCII 10 is 
 linefeed rather than newline.)

Actually, the correct name for this character is OS-dependant:
The ASCII standard prescribes that if an OS chooses to use a
single character as its line terminator, then it must be this
one, and one should call it newline.  Otherwise, it's name
is linefeed.  So, the correct name is newline on Posix
system, but linefeed on Windows.


 Similarly, other control character combinations send other control codes:
 
 ^A = ASCII 0x01 Start Of Heading
 ^L = ASCII 0xFF Formfeed \f
 ^M = ASCII 0x0D Carriage Return \r
 
 etc.
 
 http://en.wikipedia.org/wiki/C0_and_C1_control_codes
 
 
 When readline is enabled in bash, one of the standard editing commands is 
 that C-q (usually ctrl-Q on the keyboard) instructs readline to treat the 
 next key as a literal. So Ctrl-Q followed by Backspace won't delete the 
 previous character, but insert a literal DEL 0x7F character. 

It depends on what mode bash is in.  In Emacs mode, C-q works as you
describe, but in Vi mode you'd use C-v.

Doesn't everybody run bash in Vi mode :-?

 (One of those historical quirks is that on most(?) keyboards, the 
 Backspace key generates a DEL character rather than the ^H backspace 
 control code, and the Delete key generates an escape sequence. Go figure.)

Another quirk is that on most keyboards the enter key generates a
Carriage Return, which the terminal driver than converts to a Newline,
if icrlf mode is active.  (Shouldn't that be called icrnl mode?)


Hope this helps,

-- HansM

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


Re: Right solution to unicode error?

2012-11-08 Thread Hans Mulder
On 8/11/12 00:53:49, Steven D'Aprano wrote:
 This error confuses me. Is that an exact copy and paste of the error, or 
 have you edited it or reconstructed it? Because it seems to me that if 
 task.subject is a unicode string, as it appears to be, calling print on 
 it should succeed:
 
 py s = u'ABC\u2013DEF'
 py print s
 ABC–DEF

That would depend on whether python thinks sys.stdout can
handle UTF8.  For example, on my MacOS X box:

$ python2.6 -c 'print uabc\u2013def'
abc–def
$ python2.6 -c 'print uabc\u2013def' | cat
Traceback (most recent call last):
  File string, line 1, in module
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in
position 3: ordinal not in range(128)

This is because python knows that my terminal is capable
of handling UTF8, but it has no idea whether the program at
the other end of a pipe had that ability, so it'll fall
back to ASCII only if sys.stdout goes to a pipe.

Apparently the OP has a terminal that doesn't handle UTF8,
or one that Python doesn't know about.


Hope this helps,

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-08 Thread Hans Mulder
On 8/11/12 19:05:11, jkn wrote:
 Hi All
 i am trying to build up a set of subprocess.Ponen calls to
 replicate the effect of a horribly long shell command. I'm not clear
 how I can do one part of this and wonder if anyone can advise. I'm on
 Linux, fairly obviously.
 
 I have a command which (simplified) is a tar -c command piped through
 to xargs:
 
 tar  -czvf myfile.tgz -c $MYDIR mysubdir/ | xargs -I '{}' sh -c test -
 f $MYDIR/'{}'
 
 (The full command is more complicated than this; I got it from a shell
 guru).
 
 IIUC, when called like this, the two occurences of '{}' in the xargs
 command will get replaced with the file being added to the tarfile.
 
 Also IIUC, I will need two calls to subprocess.Popen() and use
 subprocess.stdin on the second to receive the output from the first.
 But how can I achive the substitution of the '{}' construction across
 these two calls?

That's what 'xargs' will do for you.  All you need to do, is invoke
xargs with arguments containing '{}'.  I.e., something like:

cmd1 = ['tar', '-czvf', 'myfile.tgz', '-c', mydir, 'mysubdir']
first_process = subprocess.Popen(cmd1, stdout=subprocess.PIPE)

cmd2 = ['xargs', '-I', '{}', 'sh', '-c', test -f %s/'{}' % mydir]
second_process = subprocess.Popen(cmd2, stdin=first_process.stdout)

 Apologies if I've made any howlers in this description - it's very
 likely...

I think the second '-c' argument to tar should have been a '-C'.

I'm not sure I understand what the second command is trying to
achieve.  On my system, nothing happens, because tar writes the
names of the files it is adding to stderr, so xargs receives no
input at all.  If I send the stderr from tar to the stdin of
xargs, then it still doesn't seem to do anything sensible.

Perhaps your real xargs command is more complicated and more
sensible.



Hope this helps,

-- HansM

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


Re: Base class and Derived class question

2012-11-06 Thread Hans Mulder
On 6/11/12 14:47:03, cyberira...@gmail.com wrote:
 Hey guys,
 I'm trying to understand how is working base class and derived class.
 So, I have to files baseClass.py and derivedClass.py. 
 baseClass.py : 
 [CODE]class baseClass():
 def bFunction(self):
 print We are in a base class[/CODE]
 
 derivedClass.py:
 [CODE]import baseClass as baseClassMod
 reload(baseClassMod)
 
 class derivedClass(baseClassMod):

This line is wrong: baseClassMod is a module, not a class.
You cannot derive a class from a module, only from another class.

If you import baseClass as baseClassMod, then the name of the class
defined inside the module is baseClassMod.baseClass, so this line
should be:

class derivedClass(baseClassMod.baseClass):

 def dFunction(self):
 print We are in a derived Class [/CODE]
 
 buwhen I'm trying to run derivedClass.py I get this error :
 [CODE][COLOR=Red]TypeError: Error when calling the metaclass bases
 module.__init__() takes at most 2 arguments (3 given)[/COLOR][/CODE]
 
 Interesting thing is that if I run baseClass.py and then run :
 [CODE]class derivedClass(baseClass):
 def dFunction(self):
 print We are in a derived Class[/CODE]
 It works fine

Alternative solutions that also work:

import baseClass

class derivedClass(baseClass.baseClass):
def dFunction(self):
print We are in a derived Class

Or you can import the class rather than the module:

from baseClass import baseClass

class derivedClass(baseClass):
def dFunction(self):
print We are in a derived Class


If you're trying to learn about derived classes, then it might
be a good idea to avoid learning about the pitfalls of importing
at the same time.  If you simply put both classes in the same
file, the problem disappears:

class baseClass():
def bFunction(self):
print We are in a base class

class derivedClass(baseClass):
def dFunction(self):
print We are in a derived Class

instance = derivedClass()
instance.bFunction()
instance.dFunction()

This works as you'd expect.


Incidentally, the recommended way to avoid confusion between modules
and classes it to use lower case names for your modules abd names
with an initial capital for your classes, for example:

class BaseClass(object):
def bMethod(self):
print We are in a base class

class DerivedClass(BaseClass):
def dMethod(self):
print We are in a derived Class

instance = DerivedClass()
instance.bMethod()
instance.dMethod()


Hope this helps,

-- HansM





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


Re: Multi-dimensional list initialization

2012-11-05 Thread Hans Mulder
On 5/11/12 07:27:52, Demian Brecht wrote:
 So, here I was thinking oh, this is a nice, easy way to initialize a 4D 
 matrix
 (running 2.7.3, non-core libs not allowed):
 
 m = [[None] * 4] * 4
 
 The way to get what I was after was:
 
 m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] 

Or alternateively:

m = [[None] * 4 for _ in range(4)]

 (Obviously, I could have just hardcoded the initialization, but I'm too
 lazy to type all that out ;))
 
 The behaviour I encountered seems a little contradictory to me.
 [None] * 4 creates four distinct elements in a single array

Actually, it creates a list with four references to the same object.
But then, this object is immutable, so you won't notice that it's the
same object.

 while [[None] * 4] * 4 creates one distinct array of four distinct
 elements, with three references to it:

We usually phrase that as a list with four references to the
same list.  The first reference is not special in any way.

 a = [None] * 4
 a[0] = 'a'
 a
 ['a', None, None, None]
 
 m = [[None] * 4] * 4
 m[0][0] = 'm'
 m
 [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], 
 ['m', None, None, None]]
 
 Is this expected behaviour

Yes.

 and if so, why? In my mind either result makes sense, but the
 inconsistency is what throws me off.

There's no inconsistency: in both cases you get a list with four
references to the same object.  The only difference is that in the
fist case, the references are to an immutable object, so the fact
that it's the same object won't hurt you.


Hope this helps,

-- HansM

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


Re: is implemented with id ?

2012-11-04 Thread Hans Mulder
On 4/11/12 06:09:24, Aahz wrote:
 In article mailman.3250.1351999198.27098.python-l...@python.org,
 Chris Angelico  ros...@gmail.com wrote:
 On Sun, Nov 4, 2012 at 2:10 PM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:

 /* Shortcut for empty or interned objects */
 if (v == u) {
 Py_DECREF(u);
 Py_DECREF(v);
 return 0;
 }
 result = unicode_compare(u, v);

 where v and u are pointers to the unicode object.

 There's a shortcut if they're the same. There's no shortcut if they're
 both interned and have different pointers, which is a guarantee that
 they're distinct strings. They'll still be compared char-for-char
 until there's a difference.
 
 Without looking at the code, I'm pretty sure there's a hash check first.

In 3.3, there is no such check.

It was recently proposed on python-dev to add such a check,
but AFAIK, no action was taken.

-- HansM


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


Re: is implemented with id ?

2012-11-03 Thread Hans Mulder
On 3/11/12 20:41:28, Aahz wrote:
 [got some free time, catching up to threads two months old]
 
 In article 50475822$0$6867$e4fe5...@news2.news.xs4all.nl,
 Hans Mulder  han...@xs4all.nl wrote:
 On 5/09/12 15:19:47, Franck Ditter wrote:

 - I should have said that I work with Python 3. Does that matter ?
 - May I reformulate the queston : a is b and id(a) == id(b)
   both mean : a et b share the same physical address. Is that True ?

 Yes.

 Keep in mind, though, that in some implementation (e.g.  Jython), the
 physical address may change during the life time of an object.

 It's usually phrased as a and b are the same object.  If the object
 is mutable, then changing a will also change b.  If a and b aren't
 mutable, then it doesn't really matter whether they share a physical
 address.
 
 That last sentence is not quite true.  intern() is used to ensure that
 strings share a physical address to save memory.

That's a matter of perspective: in my book, the primary advantage of
working with interned strings is that I can use 'is' rather than '=='
to test for equality if I know my strings are interned.  The space
savings are minor; the time savings may be significant.

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


Re: csv read clean up and write out to csv

2012-11-02 Thread Hans Mulder
On 2/11/12 18:25:09, Sacha Rook wrote:
 I have a problem with a csv file from a supplier, so they export data to csv
 however the last column in the record is a description which is marked up
 with html.
 
 trying to automate the processing of this csv to upload elsewhere in a
 useable format. If i open the csv with csved it looks like all the records
 aren't escaped correctly as after a while i find html tags and text on the
 next line/record.

The example line you gave was correctly escaped: the description starts
with a double quote, and ends several lines later with another double
quote.  Double quotes in the HTML are represented by 'quot;'.

Maybe csved doesn't recognize this escape convention?

 If I 'openwith' excel the description stays on the correct line/record?

Excel implements this convention

 I want to use python to read these records in and output a valid csv with
 the descriptions intact preferably without the html tags so a string of
 text formatted with newline/CR where appropriate.

How about this:

import csv

infile = file(input.csv, rb)
outfile = file(output.csv, wb)

reader = csv.reader(infile)
writer = csv.writer(outfile)

for line in reader:
line[-1] = line[-1].replace(\n,  )
print line
writer.writerow(line)

infile.close()
outfile.close()


That will replace the newlines inside the HTML, which your csved
doesn't seem to recognize, by spaces.  When viewed as HTML code,
spaces have the same effect as newlines, so this replacement
shouldn't alter the meaning of the HTML text.

Hope this helps,

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


Re: sort order for strings of digits

2012-10-31 Thread Hans Mulder
On 31/10/12 16:17:14, djc wrote:
 Python 3.2.3 (default, Oct 19 2012, 19:53:16)
 
 sorted(n+s)
 ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40',
 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
 
 sorted(int(x) if x.isdigit() else x for x in n+s)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: unorderable types: str()  int()


 sorted(n+s, key=lambda x:(x.__class__.__name__, x))
['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40',
'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']


 The best I can think of is to split the input sequence into two lists,
 sort each and then join them.

That might well be the most readable solution.


Hope this helps,

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


Re: Error compiling python3.2.3: architecture of input file is incompatible

2012-10-27 Thread Hans Mulder
On 27/10/12 16:11:48, Tobias Marquardt wrote:
 Hello,
 
 I am trying to compile Python 3.2.3.
 On my 64 bit Ubuntu machine I have no problems but using Ubuntu 32 but I
 get the following error:
 
 /usr/bin/ld: i386:x86-64 architecture of input file
 `Parser/tokenizer_pgen.o' is incompatible with i386 output
 /usr/bin/ld: i386:x86-64 architecture of input file
 `Parser/printgrammar.o' is incompatible with i386 output
 /usr/bin/ld: i386:x86-64 architecture of input file `Parser/pgenmain.o'
 is incompatible with i386 output
 collect2: ld returned 1 exit status
 make: *** [Parser/pgen] Error 1
 
 As it's the first time for me compiling python by myself, I have no idea
 how to solve this. So I'm glad to see any suggestions.

It looks like you've copied *.o files from your 64 bit build
to your 32 bit box.  If that's your problem, then the easiest
solution is to delete everything and start over.

Alternatively, you could run make distclean; that will delete
a lot of generated files and then re-run configure.  After that,
running make should work.


Hope this helps,

-- HansM



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


Re: resume execution after catching with an excepthook?

2012-10-25 Thread Hans Mulder
On 24/10/12 14:51:30, andrea crotti wrote:
 So I would like to be able to ask for confirmation when I receive a C-c,
 and continue if the answer is N/n.
 
 I'm already using an exception handler set with sys.excepthook, but I
 can't make it work with the confirm_exit, because it's going to quit in
 any case..
 
 A possible solution would be to do a global try/except
 KeyboardInterrupt, but since I already have an excepthook I wanted to
 use this.  Any way to make it continue where it was running after the
 exception is handled?
 
 
 def confirm_exit():
 while True:
 q = raw_input(This will quit the program, are you sure? [y/N])
 if q in ('y', 'Y'):
 sys.exit(0)
 elif q in ('n', 'N'):
 print(Continuing execution)
 # just go back to normal execution, is it possible??
 break
 
 
 def _exception_handler(etype, value, tb):
 if etype == KeyboardInterrupt:
 confirm_exit()
 else:
 sys.exit(1)
 
 
 def set_exception_handler():
 sys.excepthook = _exception_handler

I think the trick is to not use an except hook, but trap the
interrupt on a lower level.

This seems to work; I'm not sure how robust it is:

import signal

def handler(signum, frame):
while True:
q = raw_input(This will quit the program, are you sure? [y/N])
if q[:1] in yY:
raise KeyboardInterrupt
elif q[:1] in nN:
print(Continuing execution)
# just go back to normal execution
return

signal.signal(signal.SIGINT, handler)


If you're debugging this on a Unix platform, it may help to know
that you can also kill a process with control-\

Hope this helps,

-- HansM


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


Re: 'generator ignored GeneratorExit''

2012-10-20 Thread Hans Mulder
On 21/10/12 01:41:37, Charles Hixson wrote:
 On 10/20/2012 04:28 PM, Ian Kelly wrote:
 On Sat, Oct 20, 2012 at 2:03 PM, Charles Hixson
 charleshi...@earthlink.net  wrote:
 If I run the following code in the same module, it works correctly,
 but if I
 import it I get the message:
 Exception RuntimeError: 'generator ignored GeneratorExit' ingenerator
 object getNxtFile at 0x7f932f884f50  ignored

 def getNxtFile (startDir, exts = [txt, utf8]):
  try:
  forpathingetNxtPath (startDir, exts):
  try:
  fil=open (path, encoding = utf-8-sig)
  yieldfil
  except:
  print (Could not read:  , path)
  exceptGeneratorExit:
  raiseStopIteration

 The message appears to be purely informational, but I *would* like to
 fix
 whatever problem it's reporting, and none of the changes that I've tried
 have worked.  What *should* I be doing?
 The bare except is probably catching the GeneratorExit exception and
 swallowing it.  Try catching a more specific exception like OSError or
 even just Exception instead.

 Also, you don't need to explicitly catch GeneratorExit just to raise
 StopIteration.  The generator will normally stop on GeneratorExit,
 provided the exception is actually able to propagate up.
 Thank you.  That was, indeed the problem.  Removing all the try ...
 excepts made it work on my test case.  Now I've got to figure out what
 to catch in case it's not a utf8 file.  I guess that I'll hope that
 IOError will work, as nothing else sounds reasonable.  It's general
 enough that it ought to work.

I would expect a UnicideError, which is a subclass of ValueError,
but not of IOError.  And I'd expect it to be raised when you start
reading the file, not when you open it.

The easiest way to find out this sort of thing, is to not have any
except clauses in your code.  That way, the interpreter will tell
you exactly what was raised, and where.  Much easier than guessing.

 import sys
 fil = open(sys.executable, encoding=utf8)
 fil.readline()
Traceback (most recent call last):
  File stdin, line 1, in module
  File
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/codecs.py,
line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0:
invalid continuation byte

As I expected, opening a non-utf8 file succeeds, but readline() fails.

Hope this helps,

-- HansM


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


Re: error executing import html.parser from a script

2012-10-19 Thread Hans Mulder
On 19/10/12 11:15:45, Paul Volkov wrote:
 What is this madness?

That's because your script is called html.py.

If you import html.parser, Python first imports html,
then checks that it's a package and contains a module
named parser.  When Python imports html, it searches
for a file named html.py.  It finds your script,
imports it and decides that it's not a package.

Solution: rename your script.

 I have Python 3.3.0 installed on Windows XP. I do not have Python 2
 (but I had it before). I do the following steps:
 
 1. Import from an interactive session (no problems)
 
 python
 Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
 32 bit (Intel)] on win32
 Type help, copyright, credits or license for more information.
 import html
 import html.parser
 ^Z
 
 2. Import from command line (no problems)
 
 python -c import html.parser
 
 3. Import from a script. The script contains only one import line.
 Everything else is commented out.
 
 python e:\tmp\pyt\html.py
 Traceback (most recent call last):
   File frozen importlib._bootstrap, line 1512, in _find_and_load_unlocked
 AttributeError: 'module' object has no attribute '__path__'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File e:\tmp\pyt\html.py, line 7, in module
 import html.parser
   File e:\tmp\pyt\html.py, line 7, in module
 import html.parser
 ImportError: No module named 'html.parser'; html is not a package
 
 4. And then I tried to print sys.path from my script by modifying it this way:
 import sys
 print (sys.path)
 import html.parser
 
 And the result is: (I don't know why sys.path is printed twice)

The first time, your script is being run as a script, and
has the module name '__main__'.  The second time, your script
is being imported as a module, and has the module name 'html'.

When Python finds the command import html.parser for the
second time, there's an (incomplete) module named html in
sys.modules, and Python doesn't try to import html again
and instead tries to use it as a package, and fails.

 python e:\tmp\pyt/html.py
 ['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
 'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
 D:\\Python33', 'D:\\Python33\\lib\\site-packages']
 ['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
 'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
 D:\\Python33', 'D:\\Python33\\lib\\site-packages']
 Traceback (most recent call last):
   File frozen importlib._bootstrap, line 1512, in _find_and_load_unlocked
 AttributeError: 'module' object has no attribute '__path__'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File e:\tmp\pyt/html.py, line 8, in module
 import html.parser
   File e:\tmp\pyt\html.py, line 8, in module
 import html.parser
 ImportError: No module named 'html.parser'; html is not a package

Notice how the import is also being reported twice.


Hope this helps,

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


Re: A desperate lunge for on-topic-ness

2012-10-18 Thread Hans Mulder
On 18/10/12 08:31:51, Steven D'Aprano wrote:
 On Thu, 18 Oct 2012 02:06:19 -0400, Zero Piraeus wrote:
 3. Say well, at least it's not a backslash and break the line using
  parentheses.
 I mostly do this. Since most lines include a bracket of some sort, I 
 rarely need to add outer parentheses just for the purpose of line 
 continuation.
 
 some_variable = spam('x') + ham(
 some_longer_variables, here_and_here, 
 and_here_also)

I would spell that as:

some_variable = spam('x') + ham(
some_longer_variables,
here_and_here,
and_here_also,
)

 I know PEP 8 says I should drop the final round bracket to the next line, 
 but I don't normally like that.

I normally put the final bracket on the next line, where it is
very visible.  Compare:

if looks_like_it_might_be_spam(
some_longer_variables,
here_and_here, and_here_also):
logger.notice(might be spam)
move_to_spam_folder(some_longer_variables)
update_spam_statistics(here_and_here)

vs.

if looks_like_it_might_be_spam(
some_longer_variables,
here_and_here,
and_here_also,
):
logger.notice(might be spam)
move_to_spam_folder(some_longer_variables)
update_spam_statistics(here_and_here)

Which one would you say is more readable?


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


Re: list comprehension question

2012-10-17 Thread Hans Mulder
On 17/10/12 09:13:57, rusi wrote:
 On Oct 17, 10:22 am, Terry Reedy tjre...@udel.edu wrote:
 On 10/16/2012 9:54 PM, Kevin Anthony wrote:

 I've been teaching myself list comprehension, and i've run across
 something i'm not able to convert.

 list comprehensions specifically abbreviate the code that they are
 (essentially) equivalent to.

 res = []
 for item in source:
res.append(f(item))
 res

 ==

 [f(item) for item in source]

 Matrix multiplication does not fit the pattern above. The reduction is
 number addition rather than list appending.
 
 Dunno why you say that. Heres matrix multiply using list
 comprehensions:
 
 from operator import add
 def dot(p,q): return reduce(add, (x*y for x,y in zip(p,q)))
 
 def transpose(m): return zip(*m)
 
 def mm(a,b): return mmt(a, transpose(b))
 
 def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a]
 
 which can then be 'reduced' to a one-liner if that takes your fancy

I can golf it down to two lines without losing readability:

def dot(p,q): return sum(x*y for x,y in zip(p,q))

def mm(a,b): return [[dot(ra, rb) for rb in zip(*b)] for ra in a]


Hope this helps,

-- HansM

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


Re: bad httplib latency due to IPv6 use

2012-10-17 Thread Hans Mulder
On 17/10/12 09:55:13, Ulrich Eckhardt wrote:
 Hi!
 
 I noticed yesterday that a single HTTP request to localhost takes
 roughly 1s, regardless of the actually served data, which is way too
 long. After some digging, I found that the problem lies in
 socket.create_connection(), which first tries the IPv6 ::1 and only then
 tries the IPv4 127.0.0.1. The first one times out after 1s, causing the
 long latency.
 
 What I'm wondering is this:
 1. The server only serves on IPv4, changing this to IPv6 would probably
 help. However, I wouldn't consider this a bug, or?

I'd say it's a bug in your TCP/IP stack.  An IP shouldn't take that long
to figure out that it is not configured to connect to IPv6 addresses.

 2. I don't even have any IPv6 addresses configured and I'm not using
 IPv6 in any way, so why does it try those at all?

I have no experience with win7/64, but on earlier versions of Windows,
there's a file named hosts, somewhere in a system directory.  When
looking up an IP address, this file is consulted first.  Removing the
::1 from the entry for localhost might be a sensible work-around.

 3. Of course I can optimize the code for IPv4, but then I would be
 pessimizing IPv6 and vice versa...

I'd avoid doing that, if at all possible.

 Any other suggestions?

Try ping localhost on the command line.  It that has the same problem,
than the problem is not in Python, but in your IP stack. It might be in
a FAQ list for some windows-specific forum.

 Notes:
  * Using 127.0.0.1 as host works without the delay.

That make sense: it's the attempt to connect to ::1 that is the problem.

  * I'm using Python 2.7 on win7/64bit


Hope his helps,

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


Re: Index in a list

2012-10-17 Thread Hans Mulder
On 17/10/12 12:10:56, Anatoli Hristov wrote:
 I'm trying to index a text in a list as I'm importing a log file and
 each line is a list.
 
 What I'm trying to do is find the right line which contains the text
 User : and take the username right after the text User :, but the
 list.index((User :) is indexing only if all the text matching. How
 can I have the right position of the line which contains the word
 ((User :)

Perhaps something like:

target = (User :
for line in your_list:
position = line.find(target)
if position = 0:
print line[position+len(target):]


Hope this helps,

-- HansM

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


Re: cx_Oracle clause IN using a variable

2012-10-16 Thread Hans Mulder
On 16/10/12 15:41:58, Beppe wrote:
 Hi all,
 I don't know if it is the correct place to set this question, however,
 I'm using cx_Oracle to query an Oracle database.
 I've a problem to use the IN clause with a variable.
 My statement is 
 
 sql = SELECT field1,field2,field3
 FROM my_table
 WHERE field_3 IN (:arg_1)
 
 where arg_1 is retrive by a dictionary
 that is build so
 
  my_dict = {'location':X,
 'oracle_user':'user',
 'oracle_password':'pass',
 'dsn':'dsn',
 'mailto':'some...@somewhere.org',
 'codes':CNI,CNP}
 
 args = (dict['codes'],)

  
 con = cx_Oracle.connect(my_dict[oracle_user],
  my_dict[oracle_password],
  my_dict[dsn])
  
 cur = con.cursor()
 cur.execute(sql,args)
 rs =  cur.fetchall()   
 
 but it doesn't work in the sense that doesn't return anything
 
 If i use the statment without variable 
 
 SELECT field1,field2,field3
 FROM my_table
 WHERE field_3 IN ('CNI','CNP')
 
 the query works
 
 what is wrong?

You only have a single placeholder variable,
so your statement is equivalent to

SELECT field1,field2,field3
FROM my_table
WHERE field_3 IN ('CNI,CNP')

Presumably 'CNI,CNP' is not a valid value for field_3,
thus your query finds no records.

 suggestions?

To verify that you have the correct syntax, try it
with a single value first:

my_dict = {'location':X,
'oracle_user':'user',
'oracle_password':'pass',
'dsn':'dsn',
'mailto':'some...@somewhere.org',
'codes':CNI}

It that produces some of the records you want, then the
question is really: can you somehow pass a list of values
via a single placeholder variable?

I'm, not a cx_Oracle expert, but I think the answer is no.


If you want to pass exactly two values, then the work-around
would be to pass them in separate variables:

my_dict = {'location':X,
'oracle_user':'user',
'oracle_password':'pass',
'dsn':'dsn',
'mailto':'some...@somewhere.org',
'code1':CNI,
'code2':CNP}

sql = SELECT field1,field2,field3
 FROM my_table
 WHERE field_3 IN (:arg_1, :arg_2)
args = (my_dict['code1'],my_dict['code2'])


If the number of codes can vary, you'll have to generate a
query with the correct number of placholders in it.  Mabye
something like this (untested):

my_dict = {'location':X,
'oracle_user':'user',
'oracle_password':'pass',
'dsn':'dsn',
'mailto':'some...@somewhere.org',
'codes':Ornhgvshy,vf,orggre,guna,htyl}


args = my_dict['codes'].split(,)
placeholders = ','.join(:x%d % i for i,_ in enumerate(args))

sql = SELECT field1,field2,field3
 FROM my_table
 WHERE field_3 IN (%s) % placeholders

con = cx_Oracle.connect(my_dict[oracle_user],
 my_dict[oracle_password],
 my_dict[dsn])

cur = con.cursor()
cur.execute(sql,args)
rs =  cur.fetchall()


Hope this helps,

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


Re: Insert item before each element of a list

2012-10-11 Thread Hans Mulder
On 9/10/12 04:39:28, rusi wrote:
 On Oct 9, 7:34 am, rusi rustompm...@gmail.com wrote:
 How about a 2-paren version?

 x = [1,2,3]
 reduce(operator.add,  [['insert', a] for a in x])

 ['insert', 1, 'insert', 2, 'insert', 3]
 
 Or if one prefers the different parens on the other side:
 
 reduce(operator.add, (['insert', a] for a in x))
 ['insert', 1, 'insert', 2, 'insert', 3]

Or, if you don't want to import the operator module:

sum((['insert', a] for a in x), [])

-- HansM

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


Re: How to create a login screen using core python language without using any framework

2012-10-05 Thread Hans Mulder
On 5/10/12 10:03:56, shivakrsh...@gmail.com wrote:
 I need to develop a simple login page using Python language with
 two fields and a button, like:
 
 Username, Password, Login
 
 I know there are some beautiful Python frameworks like
 
 Django, Grok, WebPy, TurboGears

 which support web development using Python, but mine is a basic
 requirement consisting of only 3 screens (pages):
 
 * 1st page  -  Login page (Redirects to 2nd page when login button
   is clicked) 
 * 2nd page  -  Page with records in the form of a list, with an
   option for adding new records (Redirects to 3rd page when Add
   Records button is clicked)
 * 3rd page  -  Page with fields, which are saved as records for
   the list on 2nd page  (After entering details and clicking Submit)

Implementing your application using any of those frameworks you
mentioned, would be easy.

 So, I have decided to develop the above functionality using Python
 **without** using any framework, so that I can have flexibility as
 well as write my own code. 

This is a bad idea.  You'll get much more flexibility using
an existing framework then you'd ever achieve by reinventing
the wheel on your own.  Especially if you have no experience
in this field.

 1. Is it possible to create a login page using Python without
 using a framework?

Yes.

But it's a lot of work.  You'd effectively be rewriting all
the functionality you'd get for free with a framework.  And
it wouldn't be as flexible, because frameworks can flex in
directions that you didn't think of.

 2. I haven't worked on web services and don't know the basics of
 web development in Python.

In that case, your chances of success are fairly slim.

 3. If possible, can you provide me an example on how to create a
 login page using Python and achieve the functionality described
 above?

The frameworks you mentioned earlier come with tutorials.
These tutorials contain such examples.

You should really use an existing framework.  Once you're
aware of how much functionality you get out of a framework
(any of them), you wouldn't dream of rewriting all that
functionality on your own.


Hope this helps,

-- HansM





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


Re: error bluetooth

2012-10-05 Thread Hans Mulder
On 5/10/12 10:51:42, Luca Sanna wrote:

 from bluetooth import *

[..]

 luca@luca-XPS-M1330:~/py-temperature/py-temperature$ python bluetooth.py

When you say from bluetooth import *, Python will find a file
name bluetooth.py and import stuff from that file.  Since your
script happens to be named bluetooth.py, Python will import
your script, thinking it is a module.

 it's a bug of the module?

You've chosen the wrong file name.  Rename your script.


Hope this helps,

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


Re: Can somebody give me an advice about what to learn?

2012-10-03 Thread Hans Mulder
On 1/10/12 00:14:29, Roy Smith wrote:
 In article mailman.1677.1349019431.27098.python-l...@python.org,
  Chris Angelico ros...@gmail.com wrote:
 
 you can't, for instance, retain a socket connection object across 
 that sort of reload.
 
 Yeah, that's a problem.  There's nothing fundamental about a TCP 
 connection endpoint which precludes it being serialized and passed 
 around.  The amount of state involved is pretty small.  Unless I've 
 forgotten something, 2 IP addresses, 2 port numbers, a few bits worth of 
 TCP protocol state, and, for open connections, 2 sequence numbers.  
 Maybe a couple of timers, but I don't think they're strictly necessary.  
 The problem is, most of that state is private to the kernel.

You're looking at the wrong abstraction level.  A socket connection
object is a thin wrapper around a file descriptor.  Most posix platforms
allow you to pass file descriptors to other processes running on the
same box.  Thus, most posix platforms *do* allow you to pass socket
connection objects around, though you won't win prizes for portability.

 On the other hand, you could do what screen does, and spawn a process 
 per connection to hold the connection open :-)

That won't work for the sort of application we're discussing, because
it creates too many processes.  A pool of processes, each handling
many connections, might work though.

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


Re: parse an environment file

2012-10-01 Thread Hans Mulder
On 1/10/12 16:12:50, Jason Friedman wrote:
 I want my python 3.2.2 script, called via cron, to know what those
 additional variables are.  How?
 
 Thank you for the feedback.  A crontab line of
 
 * * * * * . /path/to/export_file  /path/to/script.py
 
 does indeed work, but for various reasons this approach will not
 always be available to me.
 
 Let me restate my question.  I have a file that looks like this:
 export VAR1=foo
 export VAR2=bar
 # Comment
 export VAR3=${VAR1}${VAR2}
 
 I want this:
 my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}
 
 I can roll my own, but I'm thinking there is a module or existing code
 that does this.  I looked at the os and sys and configparse modules
 but did not see it.

One tactic is to write a wrapper script in shellese that sets the
variables and then runs your script.  Something like:

#/bin/bash
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

# Read some more settings from a file
. /path/to/file/with/more/exports

# Drum roll .
/path/to/your/script.py


This allows you to copy-and-paste all sorts of weird and wonderful
shell syntax into your wrapper script.

AFAIK, there is no Python module that can read shell syntax.
You could translate all that shell syntax manually to Python,
but that may not be worth the effort.

Hope this helps,

-- HansM

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


Re: Can't import modules

2012-09-30 Thread Hans Mulder
On 30/09/12 21:42:37, Peter Farrell wrote:
 I'm still new to Python, so here's another easy one. After I save something
 I've done as a .py file, how do I import it into something else I work on?
 Every time I try to import something other than turtle or math, I get this 
 error message:
 
 'module' object is not callable
 
 What am I doing wrong?

For starters, you're not showing us any code.

The error message suggests that you have successfully imported
a module, and you then try to use the module as if it were a
callable.  That won't work: modules are not callable.

My crystal ball says that you may have been a Java programmer
in an earlier life.  In Java, a file must define exactly one
class, and the class must have the same name as the file.

Python is not Java.  In Python, a file may define one class,
or twenty, or none at all.  To avoid confusion, do not give
any of your classes the same name as any of your files.


Hope this helps,

-- HansM


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


Re: Reducing cache/buffer for faster display

2012-09-29 Thread Hans Mulder
On 29/09/12 02:20:50, Rikishi42 wrote:
 On 2012-09-28, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 On Thu, 27 Sep 2012 22:25:39 + (UTC), John Gordon gor...@panix.com
 declaimed the following in gmane.comp.python.general:


 Isn't terminal output line-buffered?  I don't understand why there would
 be an output delay.  (Unless the \r is messing things up...)

  It's the trailing , The \r is being used to reset to the
 beginning of the console line, but the comma says more output for
 /this/ line will be coming... So no output until explicitly flushed, or
 a new-line is issued.
 
 Well, the \r seems to be the problem, allright.
 But output was not completely blocked, just delayed a very long time.  
 
 So perhaps flushing and a sending a newline aren't the only triggers for
 output.  Perhaps there's a maximum delay or a maximum cumulated size, and
 the output is flushed when such a limit is reached.

There's a maximum cumulated size; it's called the buffer size.
Output goes into a buffer, and when the buffer is full, it's
printed all at once.

One way to avoid it, is to use an unbuffered stream.

Another, more efficient, way to avoid it, is to invoke the
stream's .flush() method after writing to it.

 Anyway, that's mainly academic. I doubt there will be a correction to
 that behaviour. 

It's an optimization.  When it was invented, 40 years ago, it was a
really necessary to do this, to get something resembling performance.

The performance of a system without stream buffering would probably
be tolerable on modern hardware.  But the people maintaining Python
are unlikely to cut out buffering, because few people would benefit
(yours is pretty much the only use case where buffereing hurts) and
some would suffer (those who write many short strings to a disk file).


Hope this helps,

-- HansM

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


Re: howto handle nested for

2012-09-29 Thread Hans Mulder
On 29/09/12 03:15:24, Peter Pearson wrote:
 On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly ian.g.ke...@gmail.com wrote:

 levels = 6
 for combination in itertools.product(xrange(n_syms), levels):
 # do stuff
 
 n_syms = 3
 levels = 6
 for combination in itertools.product(xrange(n_syms), levels):
 ...   print combination
 ... 
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: 'int' object is not iterable

 n_syms = 3
 levels = 6
 for combination in itertools.product(xrange(n_syms), repeat=levels):
... print combination
...
(0, 0, 0, 0, 0, 0)
(0, 0, 0, 0, 0, 1)
(0, 0, 0, 0, 0, 2)
(0, 0, 0, 0, 1, 0)
(0, 0, 0, 0, 1, 1)
(0, 0, 0, 0, 1, 2)
(0, 0, 0, 0, 2, 0)
(0, 0, 0, 0, 2, 1)
(0, 0, 0, 0, 2, 2)
(0, 0, 0, 1, 0, 0)

etc.


Hope this helps,

-- HansM



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


Re: [Python-Dev] [RELEASED] Python 3.3.0

2012-09-29 Thread Hans Mulder
On 29/09/12 14:23:49, Amit Saha wrote:
 On Sat, Sep 29, 2012 at 10:18 PM, Georg Brandl ge...@python.org wrote:
 On behalf of the Python development team, I'm delighted to announce the
 Python 3.3.0 final release.

Thank you!!!

 For a more extensive list of changes in 3.3.0, see

  http://docs.python.org/3.3/whatsnew/3.3.html
 
 Redirects to http://docs.python.org/py3k/whatsnew/3.3.html: 404 Not Found.

Somebody has fixed it.

-- HansM




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


Re: Need to archive a MySQL database using a python script

2012-09-26 Thread Hans Mulder
On 26/09/12 01:17:24, bruceg113...@gmail.com wrote:
 Python Users Group,
 
 I need to archive a MySQL database using a python script.
 I found a good example at: https://gist.github.com/3175221
 
 The following line executes however, the archive file is empty.
 
 os.popen(mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c  %s.gz %
(user,password,host,database,database+_+filestamp))
 Where:
   User = “someUser”
   password = “somePassword”
   host = “someRemote.database.server”
   database = “someDatabase”
 
 If I execute mysqldump from the command line, an archive is created.
 
 Using Python 2.6 and MySQL-python-1.2.2.win32-py2.6 (MySQLdb)
 Mysql-5.5.27 from the command line.
 
 Any ideas?

* If there are shell meta characters in the password, you'd have
need to use single quotes, as in -p'%s'.  Actually, that's true
for any of the parameters, but the password is one most likely
to contain punctuation characters.

* You could try

print(mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c  %s.gz %
   (user,password,host,database,database+_+filestamp))

and if the result looks okay, copy and paste it to the command line
(do not retype; use copy and paste) and see if it works.

* In your script, add a line

os.popen(monty_python)

This should produce an error message.  If it doesn't, find out why.

* Check the timestamp of your empty output file.  If it was created
yesterday, then maybe your script is now writing its file in another
directory and you're looking at the output of yesterday's test.


Hope this helps,

-- HansM


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


Re: Redirecting STDOUT to a Python Variable

2012-09-23 Thread Hans Mulder
On 22/09/12 23:57:52, ross.mars...@gmail.com wrote:
 To capture the traceback, so to put it in a log, I use this
 
 import traceback
 
 def get_traceback(): # obtain and return the traceback
 exc_type, exc_value, exc_traceback = sys.exc_info()
 return ''.join(traceback.format_exception(exc_type, exc_value, 
 exc_traceback)) 

This could be coded more succinctly as

import sys, traceback

def get_traceback(): # obtain and return the traceback
return ''.join(traceback.format_exception(*sys.exc_info()))

 Suppose I have a script run by the scheduler, this captures the traceback 
 form any problems and emails them.
 
 if __name__ == '__main__':
 try: 
 Runs_unattended()
 except:
 send_mail(send_from = yourEmailAddress,
   send_to = [ yourEmailAddress ], subject = 'Runs_unattended',
   text = '%s' % get_traceback(),
   files = [], server=yourLocalSMTP)

Errhm, '%s' % get_traceback() is equiavalent to get_traceback()
How about:

if __name__ == '__main__':
try:
Runs_unattended()
except:
send_mail(send_from = yourEmailAddress,
  send_to = [ yourEmailAddress ],
  subject = 'Runs_unattended',
  text = get_traceback(),
  files = [],
  server=yourLocalSMTP)



Hope this helps,

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


Re: Exact integer-valued floats

2012-09-23 Thread Hans Mulder
On 23/09/12 01:06:08, Dave Angel wrote:
 On 09/22/2012 05:05 PM, Tim Roberts wrote:
 Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 On 22 Sep 2012 01:36:59 GMT, Steven D'Aprano wrote:
 For non IEEE 754 floating point systems, there is no telling how bad the 
 implementation could be :(
 Let's see what can be found...

 IBM 360: Same as Sigma-6 (no surprise; hearsay is the Sigma was
 designed by renegade IBM folk; even down to using EBCDIC internally --
 but with a much different interrupt system [224 individual interrupt
 vectors as I recall, vs the IBM's 7 vectors and polling to find what
 device]).
 The Control Data 6000/Cyber series had sign bit and 11-bit exponent, with
 either a 48-bit mantissa or a 96-bit mantissa, packed into one or two
 60-bit words.  Values were not automatically normalized, so there was no
 assumed 1 bit, as in IEEE-754.
 
 And it's been a long time (about 39 years), but as I recall the CDC 6400
 (at least) had no integer multiply or divide.  You had to convert to
 float first.

You didn't have to convert if your ints would fit in 48 bits.
If that was the case, then using the float multiply and divide
instructions would do the trick.

 The other oddity about the CDC series is it's the last machine I've
 encountered that used ones-complement for ints, with two values for zero.

Floats still have 0.0 and -0.0, even in IEEE-754.


Was Python ever ported to such unusual hardware?


-- HansM



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


Re: Reading a file in IDLE 3 on Mac-Lion

2012-09-22 Thread Hans Mulder
On 22/09/12 09:30:57, Franck Ditter wrote:
 In article 505ccdc5$0$6919$e4fe5...@news2.news.xs4all.nl,
  Hans Mulder han...@xs4all.nl wrote:
 
 On 21/09/12 16:29:55, Franck Ditter wrote:
 I create a text file utf-8 encoded in Python 3 with IDLE (Mac Lion).
 It runs fine and creates the disk file, visible with
 TextWrangler or another.
 But I can't open it with IDLE (its name is greyed).
 IDLE is supposed to read utf-8 files, no ?
 This works on Windows-7.

 There's a little pop-menu below the list of files.

 It allows you to choose which kind of files you want to open.
 By default, it is set to Python files, which greys out all
 files, except those with a '.py' or '.pyw' extension.
 Setting it to Text files should help, or else try All files.

 Hope this helps

 -- HansM
 
 Alas this pop-up menu is for Windows only, I don't
 find it on MacOS-X.

It's there on my MacOS X 10.6.5 system.

If your 10.7 system doesn't show it, that's definitely a bug.

 My files are xxx.dat files and not visible,
 even text only (numeric data).

As a work-around, you could name the your file xxx.pyw.

On Windows, there's a functional difference between .py
and .pyw.  On a Mac, there's no functional difference and
Idle is willing to open both types of files, so you could
use .py for code and .pyw for data.

 This can be filed as something to do !

If you're feeling adventurous, you could try solving it
yourself.  Idle is written in pure Python; that makes
this sort of thing a lot easier than if it were in C.

And bug reports with a patch are far more likely to be
picked up by the dev team.

Hope this helps,

-- HansM


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


Re: how to do draw pattern with python?

2012-09-22 Thread Hans Mulder
On 21/09/12 19:32:20, Ian Kelly wrote:
 On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán sulfur...@gmail.com wrote:
 2012/9/21 Peter Otten __pete...@web.de:
 echo.hp...@gmail.com wrote:

 print \x1b[2J\x1b[0;0H # optional

 Nice code : )

 Could you dissect that weird string for us?

 It isn't returning the cursor to (0,0), it's just like executing
 clear(1), and looks like those line coloring scape sequences for bash.
 
 They're called ANSI escape codes. :-)
 
 CSI 2J clears the screen.
 CSI 0;0H means move the cursor to row 0, column 0.  However, I don't
 think that's valid ANSI, as the coordinates are 1-based.  Probably it
 should have been \x1b[2J\x1b[1;1H.

Yes, the coordinates are 1-base, so it should have been
\x1b[2J\x1b[1;1H.  Or, since 1;1 is the default, \x1b[2J\x1b[H.

On my machine, clear(1) uses \x1b[H\x1b[2J.

Using clear(1) appears to be the most portable way to do it:

import os, time

data = \
xx
.x..x.
..xx..
..xx..
.x..x.
xx

.splitlines()

data = [line * 12 for line in data] # optional

try:
while True:
os.system(clear) # optional
for i, line in enumerate(data):
print line
data[i] = line[1:] + line[:1]
time.sleep(.1)
except KeyboardInterrupt:
pass



Hope this helps,

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


Re: portable way of locating an executable (like which)

2012-09-21 Thread Hans Mulder
On 21/09/12 04:31:17, Dave Angel wrote:
 On 09/20/2012 06:04 PM, Jason Swails wrote:
 On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N gelon...@gmail.com wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which

 The function should work under Linux and under windows.

 Did anybody already implement such a function.
 If not, is there a portable way of splitting the environment variable PATH?

 I've used the following in programs I write:

 def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv(PATH).split(os.pathsep):

On Posix systems, you need to insert at this point:

if not path:
path = .

  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

 IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
 X, but not sure about windows (since I don't know if PATH works the same
 way there).
 
 I don't have a Windows machine set up right now, but I believe there are
 two more directories to search, besides the ones described in the PATH
 variable.
 
 One is the current directory, and the other is the Windows directory
 (maybe also the xxx/system32 or something).
 
 They don't have analogues in Linux or Mac, as far as I know.

On Posix system (inlcuding Linux and Mac OS X), the current
directory is not searched by default.  If there's an empty
string in os.getenv(PATH).split(os.pathsep), then the
current directory will be searched at that point in the part.


Hope this helps,

-- HansM



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


Re: Reading a file in IDLE 3 on Mac-Lion

2012-09-21 Thread Hans Mulder
On 21/09/12 16:29:55, Franck Ditter wrote:
 I create a text file utf-8 encoded in Python 3 with IDLE (Mac Lion).
 It runs fine and creates the disk file, visible with
 TextWrangler or another.
 But I can't open it with IDLE (its name is greyed).
 IDLE is supposed to read utf-8 files, no ?
 This works on Windows-7.

There's a little pop-menu below the list of files.

It allows you to choose which kind of files you want to open.
By default, it is set to Python files, which greys out all
files, except those with a '.py' or '.pyw' extension.
Setting it to Text files should help, or else try All files.

Hope this helps

-- HansM

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


Re: Exact integer-valued floats

2012-09-21 Thread Hans Mulder
On 21/09/12 22:26:26, Dennis Lee Bieber wrote:
 On 21 Sep 2012 17:29:13 GMT, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info declaimed the following in
 gmane.comp.python.general:
 

 The question is, what is the largest integer number N such that every 
 whole number between -N and N inclusive can be represented as a float?

   Single precision commonly has 7 significant (decimal) digits. Double
 precision runs somewhere between 13 and 15 (decimal) significant digits 
 
 If my tests are correct, that value is 9007199254740992.0 = 2**53.

The expression 2 / sys.float_info.epsilon produces exactly that
number.  That's probably not a coincidence.

   For an encoding of a double precision using one sign bit and an
 8-bit exponent, you have 53 bits available for the mantissa.

If your floats have 64 bits, and you use 1 bit for the sign and 8 for
the exponent, you'll have 55 bits available for the mantissa.

 This
 ignores the possibility of an implied msb in the mantissa (encodings
 which normalize to put the leading 1-bit at the msb can on some machines
 remove that 1-bit and shift the mantissa one more place; effectively
 giving a 54-bit mantissa).

My machine has 64-bits floats, using 1 bit for the sign, 11 for the
exponent, leaving 52 for the mantissa.  The mantissa has an implied
leading 1, so it's nominally 53 bits.

You can find this number in sys.float_info.mant_dig

 Something like an old XDS Sigma-6 used
 non-binary exponents (exponent was in power of 16  2^4) and used
 non-normalized mantissa -- the mantissa could have up to three leading
 0-bits); this affected the decimal significance...

Your Sigma-6 must have sys.float_info.radix == 16 then.


Hope this helps,

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


Re: Installing Pip onto a mac os x system

2012-09-20 Thread Hans Mulder
On 20/09/12 03:32:40, John Mordecai Dildy wrote:
 Does anyone know how to install Pip onto a mac os x ver 10.7.4?
 
 Ive tried easy_instal pip but it brings up this message (but it doesn't help 
 with my problem):
 
 error: can't create or remove files in install directory
 
 The following error occurred while trying to add or remove files in the
 installation directory:
 
 [Errno 13] Permission denied: 
 '/Library/Python/2.7/site-packages/test-easy-install-1820.write-test'
 
 The installation directory you specified (via --install-dir, --prefix, or
 the distutils default setting) was:
 
 /Library/Python/2.7/site-packages/
 
 Perhaps your account does not have write access to this directory?  If the
 installation directory is a system-owned directory, you may need to sign in
 as the administrator or root account.  If you do not have administrative
 access to this machine, you may wish to choose a different installation
 directory, preferably one that is listed in your PYTHONPATH environment
 variable.
 
 For information on other options, you may wish to consult the
 documentation at:
 
   http://peak.telecommunity.com/EasyInstall.html
 
 Please make the appropriate changes for your system and try again.
 
 Thing is I am the Administrator of the computer

In that case, you should be able to do

sudo easy_instal pip

This will ask your login password, and then run easy_instal pip
with so-called root privileges.

 and can use all of the folders on the mac computer.

You may be able to view them, but I doubt that you can add
new files to folders like, say, /Library/Python .


Hope this helps,

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


Re: How to get the list of all my open file(descriptor)s and locks?

2012-09-20 Thread Hans Mulder
On 20/09/12 05:11:11, Chris Angelico wrote:
 On Thu, Sep 20, 2012 at 7:09 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 You could do:

 os.listdir(/proc/%d/fd % os.getpid())

 This should work on Linux, AIX, and Solaris, but obviously not on Windows.

On MacOS X, you can use

os.listdir(/dev/fd)

This might work on other OSes.

 I'm not sure how cross-platform it is, but at least on Linux, you can
 use /proc/self as an alias for /proc/+os.getpid() - worth a try,
 might make your code a bit easier to read.
 
 It's documented as useful when you don't know the PID yet (eg telling
 a process to read the file /proc/self/fd/0 to force it to use stdin).

At least on Linux, Solaris and MacOS X, you shouldd use /dev/stdin,
which is a symlink to whatever the official name is.
/dev/stdin is more readable and more portable than the official name.

 So I'm confident enough to recommend testing it. :)

Testing such things is the best way to learn.


Hope this helps,

-- HansM

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


Re: splitting numpy array unevenly

2012-09-19 Thread Hans Mulder
On 18/09/12 16:02:02, Wanderer wrote:
 On Monday, September 17, 2012 7:43:06 PM UTC-4, Martin De Kauwe wrote:
 On Tuesday, September 18, 2012 8:31:09 AM UTC+10, Wanderer wrote:
 I need to divide a 512x512 image array with the first horizontal
 and vertical division 49 pixels in. Then every 59 pixels in after
 that. hsplit and vsplit want to start at the edges and create a
 bunch of same size arrays. Is there a command to chop off
 different sized arrays?

 I don't know that I follow completely, but can't you just slice
 what you are after?

 x = np.random.rand(512*512).reshape(512,512)
 xx = x[0,:49]

 And put the rest of the slices in a loop...?

 I was trying to avoid the loop. I figured it out. hsplit and vsplit
 will work. I just need to give it a list of break points. I still
 need a loop though.

 breakPoints = range(49,512,59)
 rowArrays = hsplit(InputArray, breakPoints)
 OutArrays = []
 for r in rowArrays:
 OutArrays.append(vsplit(r, breakPoints))

How about a list display:

breakPoints = range(49,512,59)
rowArrays = hsplit(InputArray, breakPoints)
OutArrays = [vsplit(r, breakPoints) for r in rowArrays]

In some sense, it's still a loop, but at least it doesn't look like one.


Hope this helps,

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


  1   2   3   >