how can I check if group member exist ?

2013-06-21 Thread Hans
Hi,

I'm doing a regular expression matching, let's say 
"a=re.search(re_str,match_str)", if matching, I don't know how many str/item 
will be extracted from re_str, maybe a.group(1), a.group(2) exist but 
a.group(3) does not. 

Can I somehow check it? something like:
if exist(a.group(1)): print a.group(1)
if exist(a.group(2)): print a.group(2)
if exist(a.group(3)): print a.group(3)


I don't want to be hit by "Indexerror":
>>>print a.group(3)
Traceback (most recent call last):
  File "", line 1, in 
IndexError: no such group
>>> 

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


How should I handle socket receiving?

2011-03-11 Thread Hans
I'm thinking to write a code which to:
1. establish tons of udp/tcp connections to a server
2. send packets from each connections
3. receive packets from each connections and then do something based
on received content and connection statues.
4. repeat step 2 and step 3.

my question is how should I handle receiving traffic from each
connection respectively?  two ways I'm thinking are:
1. setup connections one by one, put socket handler into an
array(dictionary?), array also record each handler's status. then in a
big loop, keep using "select" to check sockets, if any socket get
something received, then find the socket in the array, based on the
socket status to do things should be done.  this one is like single-
thread?

2. (I don't know if this one works, but I prefer this one if it do
works. ) Setup connection object, write codes in the object to handle
sending/receiving and status. let each object to check/receive its
socket by itslef(I don't know how yet).  Then In the main proc, I just
initiate those objects.

 Any responses would be very welcome, thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How should I handle socket receiving?

2011-03-14 Thread Hans
On Mar 12, 10:13 pm, Tim Roberts  wrote:
> Hans  wrote:
>
> >I'm thinking to write a code which to:
> >1. establish tons of udp/tcp connections to a server
>
> What does "tons" mean?  Tens?  Hundreds?
>
> >my question is how should I handle receiving traffic from each
> >connection respectively?
>
> You're really going to want to use "select".  You can store the objects in
> a dictionary where the key is the socket number.  That way, you can use the
> result of the select and get your network object directly.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
my_socket=socket_thread.socket_thread(i,host,port)
my_socket.send(some_data)
my_socket.recv()

socket_thread.py
class socket_thread:
def __init__:
  self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
def send(data):
  self.soc_handle.send(data)
def recv():
  while 1:
 
input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
  data=input_list[0].recv(2048)
  print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How should I handle socket receiving?

2011-03-15 Thread Hans
On Mar 14, 1:33 pm, MRAB  wrote:
> On 14/03/2011 19:47, Hans wrote:
>
>
>
> > On Mar 12, 10:13 pm, Tim Roberts  wrote:
> >> Hans  wrote:
>
> >>> I'm thinking to write a code which to:
> >>> 1. establish tons of udp/tcp connections to a server
>
> >> What does "tons" mean?  Tens?  Hundreds?
>
> >>> my question is how should I handle receiving traffic from each
> >>> connection respectively?
>
> >> You're really going to want to use "select".  You can store the objects in
> >> a dictionary where the key is the socket number.  That way, you can use the
> >> result of the select and get your network object directly.
> >> --
> >> Tim Roberts, t...@probo.com
> >> Providenza&  Boekelheide, Inc.
>
> > I wrote code like this:
> > main proc:
> > import socket_thread
> > #start 1000 connection
> > while i<1000:
> >      my_socket=socket_thread.socket_thread(i,host,port)
> >      my_socket.send(some_data)
> >      my_socket.recv()
>
> This won't run as-is because you never assign to "i".
>
>
>
>
>
> > socket_thread.py
> > class socket_thread:
> >      def __init__:
> >            self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
> >      def send(data):
> >            self.soc_handle.send(data)
> >      def recv():
> >            while 1:
>
> > input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
> > 2)
> >                data=input_list[0].recv(2048)
> >                print data
>
> > But it does not work as I hope. main proc can only initiate one thread
> > and then trapped by it, cannot get out.
> > I'm sure I missed something but I don't know. Thanks for any help.
>
> Your "socket_thread" class is just a normal class. You create an
> instance, use it to send data, and then call its "recv" method, which
> loops forever.- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Thanks.
"i" problem is OK, I can find and fix it easily.  I also understand
the second problem, but I don't know how to fix it. What's your
meaning of "normal class"? Can I specify it as some special class and
then it can work as I hope?
thanks again.
-- 
http://mail.python.org/mailman/listinfo/python-list


how to use variable to substitute class's variable?

2011-03-17 Thread Hans
I have things like:
file1:
class aaa:
def __init__(self):
self.variable1='a1'
self.variable2='a2'
self.varable3='a3'


in main proc:
import file1
b=file1.aaa()
c={'variable1':'value1','variable2':'value2','variable3':'value3'}
for key in c:
b.key=c[key]  >>>Problem is here!!!

I hope put value1 to b.variable1, value2 to b.variable2 and value3 to
b.variable3. it does not work.  How can I do it?


By the way, I know dictionary can bind two variable together, like a 2-
dimension array.  but, if I need group 3 or more variables together,
(each group has 3 or more variables)like a 3-dimension(or higher)
array, Is there an easy way besides "class"?

Thanks a lot!!!



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


Re: how to use variable to substitute class's variable?

2011-03-17 Thread Hans
On Mar 17, 12:47 am, Benjamin Kaplan  wrote:
> On Thu, Mar 17, 2011 at 3:31 AM, Hans  wrote:
> > I have things like:
> > file1:
> > class aaa:
> >    def __init__(self):
> >        self.variable1='a1'
> >        self.variable2='a2'
> >        self.varable3='a3'
>
> > in main proc:
> > import file1
> > b=file1.aaa()
> > c={'variable1':'value1','variable2':'value2','variable3':'value3'}
> > for key in c:
> >    b.key=c[key]  >>>>>>>>>>>Problem is here!!!
>
> > I hope put value1 to b.variable1, value2 to b.variable2 and value3 to
> > b.variable3. it does not work.  How can I do it?
>
> b.key gets the "key" attribute of b, not the attribute that has the
> same name as the variable called key. Otherwise, you'd have to
> reference it as b."key" normally. If you want to dynamically set the
> variable, you'll have to use the setattr function
>
> setattr(b, key, c[key])
>
>
>
> > By the way, I know dictionary can bind two variable together, like a 2-
> > dimension array.  but, if I need group 3 or more variables together,
> > (each group has 3 or more variables)like a 3-dimension(or higher)
> > array, Is there an easy way besides "class"?
>
> A dictionary does not bind two variables together. A dictionary is a
> hash map- it maps keys to values. Each key will map to exactly one
> value. If you want to store a list of associated values, use a tuple.
> A tuple is an immutable collection of objects (the tuple itself is
> immutable, not necessarily the objects in it). It can be indexed just
> like a list.
>
>
>
> >>> l = [(0,0), (0,1,2,3,4,5,6,7), (0,1,'foo', 5 ,6)]
> >>> l[0]
> (0, 0)
> >>> l[2]
> (0, 1, 'foo', 5, 6)
> >>> l[2][1]- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Thank you VERY MUCH! it works.
-- 
http://mail.python.org/mailman/listinfo/python-list


How can I redirect or launch a html file in wsgi coding?

2013-08-15 Thread Hans
Hi, 

I have code like this:

root@lin-ser-1:~# cat /usr/local/www/wsgi-scripts/myapp.py 
def application(environ, start_response):
import sys
...

status = '200 OK'
req_method=environ['REQUEST_METHOD']
if req_method == 'POST' :
json_received = environ['wsgi.input'].read()
resp=lib_json_http.process_json(json_received)
output = simplejson.dumps(resp)
elif req_method == 'GET' :
webbrowser.open('http://autotestnet.sourceforge.net/')
return   >This code does not work
else :
 output = "invalid request method"

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

POST works OK, for GET, I hope I can redirect it to a url link, or launch a 
local html file, how can I do it? 

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


How to force a thread to stop

2006-07-22 Thread Hans
Hi all,

Is there a way that the program that created and started a thread also stops 
it.
(My usage is a time-out).

E.g.

thread = threading.Thread(target=Loop.testLoop)
thread.start() # This thread is expected to finish within a second
thread.join(2)# Or time.sleep(2) ?

if thread.isAlive():
# thread has probably encountered a problem and hangs
# What should be here to stop thread  ??

Note that I don't want to change the target (too much), as many possible 
targets exist,
together thousands of lines of code.

Thanks,
Hans 


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


Defining constant strings

2006-08-27 Thread Hans



Hi,
 
I want to define a couple of constant strings, like 
in C:
#define mystring "This is my string"
or using a const char construction.
 
Is this really not possible in Python?
 
Hans
-- 
http://mail.python.org/mailman/listinfo/python-list

tes, no reply

2005-05-11 Thread hans
z

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


win32event.WaitForInputIdle() returns too soon

2007-06-28 Thread Hans
Hi all,

I'm sending keyboard and mouse events to a seperate windows application.
I use win32event.WaitForInputIdle() before calling e.g. 
win32api.keybd_event()
However it seems that WaitForInputIdle() returns too soon because some of my
events get lost. Now I'v created my own  WaitForInputIdle() which calls the 
one
in win32event() and adds an extra sleep(0.1). As expected, this isn't 
robust.

Is this a known issue of win32event.WaitForInputIdle() ?
Is it a Windows issue?
Is more involved (but not documented in the Python docs)?
Any ideas?

Thanks
Hans 


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


Re: win32event.WaitForInputIdle() returns too soon

2007-06-29 Thread Hans

"Gabriel Genellina" <[EMAIL PROTECTED]> schreef in bericht 
news:[EMAIL PROTECTED]
> En Thu, 28 Jun 2007 19:15:40 -0300, Hans <[EMAIL PROTECTED]> escribió:
>
>> I'm sending keyboard and mouse events to a seperate windows application.
>> I use win32event.WaitForInputIdle() before calling e.g.
>> win32api.keybd_event()
>> However it seems that WaitForInputIdle() returns too soon because some 
>> of my
>> events get lost. Now I'v created my own  WaitForInputIdle() which calls
>
> From the Microsoft docs for WaitForInputIdle: "The WaitForInputIdle 
> function only works with GUI applications. If a console application calls 
> the function, it returns immediately, with no wait."
> A typical Python script is a console application.
>
> -- 
> Gabriel Genellina

It would explain my problem.
Perhaps I could create a small windows application as interface..
I have to think about it ( and wait, as I currently don't have access to
visual C++, nor the MS documentation)

Thanks,
Hans 


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

Re: win32event.WaitForInputIdle() returns too soon

2007-06-30 Thread Hans

<[EMAIL PROTECTED]> schreef in bericht 
news:[EMAIL PROTECTED]
On Jun 29, 3:03 pm, "Hans" <[EMAIL PROTECTED]> wrote:
> "Gabriel Genellina" <[EMAIL PROTECTED]> schreef in 
> berichtnews:[EMAIL PROTECTED]
>
>
>
> > En Thu, 28 Jun 2007 19:15:40 -0300, Hans <[EMAIL PROTECTED]> escribió:
>
> >> I'm sending keyboard and mouse events to a seperate windows 
> >> application.
> >> I use win32event.WaitForInputIdle() before calling e.g.
> >> win32api.keybd_event()
> >> However it seems that WaitForInputIdle() returns too soon because some
> >> of my
> >> events get lost. Now I'v created my own  WaitForInputIdle() which calls
>
> > From the Microsoft docs for WaitForInputIdle: "The WaitForInputIdle
> > function only works with GUI applications. If a console application 
> > calls
> > the function, it returns immediately, with no wait."
> > A typical Python script is a console application.
>
> > --
> > Gabriel Genellina
>
> It would explain my problem.
> Perhaps I could create a small windows application as interface..
> I have to think about it ( and wait, as I currently don't have access to
> visual C++, nor the MS documentation)
>
> Thanks,
> Hans

: Who says you have to create it with Visual C++? You could use Tkinter
: or wxPython. Both are pretty easy to pick up and can look professional
: with a little work.

: Mike


Nobody.
However, yesterday I tried wrapping my piece of offending code into a very 
simple Tkinter application.
Alas, the problem wasn't solved. WaitForInputIdle() returns too soon
(unless I don't understand WaitForInputIdle(), which is actually a Microsoft 
call.
Therefore using Visual Studio is sometimes handier: doc available and most 
compatible with Microsoft's quirks)

Any other suggestions are welcome.

Cheers, Hans


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

Re: win32event.WaitForInputIdle() returns too soon

2007-06-30 Thread Hans

"Gabriel Genellina" <[EMAIL PROTECTED]> schreef in bericht 
news:[EMAIL PROTECTED]
> En Thu, 28 Jun 2007 19:15:40 -0300, Hans <[EMAIL PROTECTED]> escribió:
>
>> I'm sending keyboard and mouse events to a seperate windows application.
>> I use win32event.WaitForInputIdle() before calling e.g.
>> win32api.keybd_event()
>> However it seems that WaitForInputIdle() returns too soon because some 
>> of my
>> events get lost. Now I'v created my own  WaitForInputIdle() which calls
>
> From the Microsoft docs for WaitForInputIdle: "The WaitForInputIdle 
> function only works with GUI applications. If a console application calls 
> the function, it returns immediately, with no wait."
> A typical Python script is a console application.
>
> -- 
> Gabriel Genellina

Well, this is weird. I searched for doc on the internet and that description 
differs:
The process you're waiting for needs bo have a message queue (it does in my 
case), not the process that calls WaitForInputIdle.
Hans

Waits until the specified process is waiting for user input with no input 
pending, or until the time-out interval has elapsed.

DWORD WINAPI WaitForInputIdle(
  HANDLE hProcess,
  DWORD dwMilliseconds
);
Parameters
  hProcess
  [in] A handle to the process. If this process is a console application or 
does not have a message queue, WaitForInputIdle returns immediately.
  dwMilliseconds
  [in] The time-out interval, in milliseconds. If dwMilliseconds is 
INFINITE, the function does not return until the process is idle.



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

how to use socket to get packet which destination ip is not local?

2010-11-16 Thread Hans
Hi,

Maybe it's a very simple question. I'm trying to write a dhcpclient
code with python. The dhcpclient does not have ip address at the very
beginning, it sends out dhcpdiscover and then server sends back
dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but
that ip is not client's local ip yet. How can I make my socket to
receive that packet?

I tried socket bind to 0.0.0.0, but it only binds to any local ip, not
any ip which may not be local. therefore the socket cannot get that
dhcp offer packet even I can use wireshark to see that packet did come
to this pc.

How to solve it? Thanks in advance.

Rgds,

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


Re: how to use socket to get packet which destination ip is not local?

2010-11-16 Thread Hans
On Nov 16, 12:57 pm, MRAB  wrote:
> On 16/11/2010 20:38, Hans wrote:
>
> > Hi,
>
> > Maybe it's a very simple question. I'm trying to write a dhcpclient
> > code with python. The dhcpclient does not have ip address at the very
> > beginning, it sends out dhcpdiscover and then server sends back
> > dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but
> > that ip is not client's local ip yet. How can I make my socket to
> > receive that packet?
>
> > I tried socket bind to 0.0.0.0, but it only binds to any local ip, not
> > any ip which may not be local. therefore the socket cannot get that
> > dhcp offer packet even I can use wireshark to see that packet did come
> > to this pc.
>
> > How to solve it? Thanks in advance.
>
> Have you read this:
>
>      http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol

Thanks for the response. Yes, I read it. it tells me how dhcp works.
But I cannot find answer for my question, dhcp response may be unicast
or broadcast depends on the flag in request. my question is how to use
socket to read that response. In my test, sniffer already shows server
sends back an offer response(unicast in my case), but because the dst-
ip in response is not local ip, then my socket cannot read it.

Can you help? thank you very much!
-- 
http://mail.python.org/mailman/listinfo/python-list


program organization question for web development with python

2010-09-15 Thread Hans
Hi,

I'm new to this area. Please allow me to ask some (maybe stupid)
questions.

I'm planning to write a web application which used for searching my
mysql database.

1. files organization

I have this in my main.py:
print """%s""" %
(record[0],table_name,cursor_name,record1)

it kind of works but not comfortable to me.

Do I have to use two files(main.py and display_tb.py)? does that means
each hyper-link needs a single file? Can I put those files together
and then they can share variables,classes, modules, etc?

2. database cursor as parameter?
I created database cursor in my main.py and then I have to use it in
another file(display_tb.py), Can I?  I put cursor as a parameter and
try to send it through hyper-link. but it somehow does not work.
error log listed below:

 /usr/lib/cgi-bin/display_tb.py in ()
   20 sql_str = "SELECT * FROM %s " % search_str_list
   21 print "%s" % sql_str
   22 cursor_ptr.execute(sql_str)
   23 result = cursor_ptr.fetchall()
   24
cursor_ptr = '',
cursor_ptr.execute undefined, sql_str = 'SELECT * FROM env_test where
id=20 '

: 'str' object has no attribute
'execute'
  args = ("'str' object has no attribute 'execute'",)
  message = "'str' object has no attribute 'execute'"

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


Re: program organization question for web development with python

2010-09-16 Thread Hans
On Sep 15, 5:33 pm, James Mills  wrote:
> On Thu, Sep 16, 2010 at 10:14 AM, Hans  wrote:
> > I'm new to this area. Please allow me to ask some (maybe stupid)
> > questions.
>
> Without reading the rest of your post too much. Designs are up to you,
> I can't comment.
>
> I can only share in a fairly common view, and that is, we'd encourage
> you to use a web framework
> as opposed to plain old CGI.
>
> cheers
> james
>
> --
> -- James Mills
> --
> -- "Problems are solved by method"

Hi James,

Thanks for response.
Maybe I did not make my question clear. I never tried python web
programing before, so I want to start from CGI.

I read something about web framework like django, but seems it's a
little bit complicated. my task is actually very simple: get search
string from input, and then search database, print search result. I
thought CGI should be good enough to do this.

I don't have any idea about how to organize those cgi codes, so what
I'm asking is:

1. do I have to have each single file for each hyper-link? Can I put
them together? how?
2. how can I pass a db_cursor to another file? can I use db_cursor as
a parameter?

Thanks again!

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


Re: program organization question for web development with python

2010-09-27 Thread Hans
On Sep 17, 2:36 am, Bruno Desthuilliers  wrote:
> Hans a écrit :
> (snip)
>
> > Maybe I did not make my question clear. I never tried python web
> > programing before, so I want to start from CGI.
>
> You can indeed learn quite a few things doing raw CGI - the most
> important one being why frameworks are a good idea !-)
>
> > I read something about web framework like django, but seems it's a
> > little bit complicated.
>
> Not that much IMHO, but being an early django user I'm probably a bit
> biased. Now Python is known as "the language with more web frameworks
> than keywords", so you could probably check some lighter framework like
>   web.py (http://webpy.org/) or flask (http://flask.pocoo.org/).
>
> > my task is actually very simple: get search
> > string from input, and then search database, print search result. I
> > thought CGI should be good enough to do this.
>
> CGI is "good enough" to do any web stuff - just like assembler is "good
> enough" to write any application !-)
>
>
>
> > I don't have any idea about how to organize those cgi codes, so what
> > I'm asking is:
>
> > 1. do I have to have each single file for each hyper-link? Can I put
> > them together? how?
>
> > 2. how can I pass a db_cursor to another file?   can I use db_cursor as
> > a parameter?
>
> Obviously not. FWIW, both questions show a lack of understanding of the
> HTTP protocol, and you can't hope to do anything good in web programming
> if you don't understand at least the basics of the HTTP protocol,
> specially the request/response cycle.
>
> Now for a couple more practical answers:
>
> There are basically two ways to organize your url => code mapping:
> 1/ have only one cgi script and use querystring params to tell which
> action should be executed.
> 2/ have one cgi script per action.
>
> The choice is up to you. For a simple app like yours, the first solution
> is probably the most obvious : always display the seach form, if the
> user submitted the form also display the result list. That's how google
> works (wrt/ user interface I mean).
>
> Now if you still need / want to have distinct scripts and want to factor
> out some common code, you just put the common code in a module that you
> import from each script.

Thank you very much! I got your meaning. The choice 1 is definitely
what I want, I just cannot think about this idea by myself. Thank you
again!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Jython and PYTHONSTARTUP

2012-06-22 Thread Hans Mulder
On 21/06/12 02:26:41, Steven D'Aprano wrote:
> There used to be a page describing the differences between Jython and 
> CPython here:
> 
> http://www.jython.org/docs/differences.html
> 
> but it appears to have been eaten by the 404 Monster.

It has been moved to:

http://www.jython.org/archive/21/docs/differences.html

Hope this helps,

-- HansM

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


Re: Frustrating circular bytes issue

2012-06-26 Thread Hans Mulder
On 26/06/12 18:30:15, J wrote:
> This is driving me batty... more enjoyment with the Python3
> "Everything must be bytes" thing... sigh...
> I have a file that contains a class used by other scripts.  The class
> is fed either a file, or a stream of output from another command, then
> interprets that output and returns a set that the main program can
> use...  confusing, perhaps, but not necessarily important.

It would help if you could post an extract that we can actually run,
to see for ourselves what happens.

> The class is created and then called with the load_filename method:
> 
> 
>  def load_filename(self, filename):
> logging.info("Loading elements from filename: %s", filename)
> 
> file = open(filename, "rb", encoding="utf-8")

When I try this in Python3, I get an error message:

ValueError: binary mode doesn't take an encoding argument


You'll have to decide for yourself whether you want to read strings or
bytes.  If you want strings, you'll have to open the file in text mode:

 file = open(filename, "rt", encoding="utf-8")

Alternatively, if you want bytes, you must leave off the encoding:


 file = open(filename, "rb")

> return self.load_file(file, filename)
> 
> As you can see, this calls the load_file method, by passing the
> filehandle and filename (in common use, filename is actually an
> IOStream object).
> 
> load_file starts out like this:
> 
> 
> def load_file(self, file, filename=""):
> elements = []
> for string in self._reader(file):
> if not string:
> break
> 
> element = {}
> 
> 
> Note that it now calls the private _reader() passing along the
> filehandle further in.  THIS is where I'm failing:
> 
> This is the private _reader function:
> 
> 
> def _reader(self, file, size=4096, delimiter=r"\n{2,}"):
> buffer_old = ""
> while True:
> buffer_new = file.read()
> print(type(buffer_new))
> if not buffer_new:
> break
> lines = re.split(delimiter, buffer_old + buffer_new)
> buffer_old = lines.pop(-1)
> 
> for line in lines:
> yield line
> 
> yield buffer_old
> 
> 
> (the print statement is something I put in to verify the problem.
> 
> So stepping through this, when _reader executes, it executes read() on
> the opened filehandle.  Originally, it read in 4096 byte chunks, I
> removed that to test a theory.  It creates buffer_new with the output
> of the read.
> 
> Running type() on buffer_new tells me that it's a bytes object.
> 
> However no matter what I do:
> 
> file.read().decode()
> buffer_new.decode() in the lines = re.split() statement
> buffer_str = buffer_new.decode()
> 
> I always get a traceback telling me that the str object has no decoe() method.
> 
> If I remove the decode attempts, I get a traceback telling me that it
> can't implicitly convert a bytes_object to a str object.
> 
> So I'm stuck in a vicious circle and can't see a way out.
> 
> here's sample error messages:
> When using the decode() method to attempt to convert the bytes object:
> Traceback (most recent call last):
>   File "./filter_templates", line 134, in 
> sys.exit(main(sys.argv[1:]))
>   File "./filter_templates", line 126, in main
> options.whitelist, options.blacklist)
>   File "./filter_templates", line 77, in parse_file
> matches = match_elements(template.load_file(file), *args, **kwargs)
>   File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
> 73, in load_file
> for string in self._reader(file):
>   File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
> 35, in _reader
> lines = re.split(delimiter, buffer_old + buffer_new.decode())
> AttributeError: 'str' object has no attribute 'decode'

Look carefulle at the traceback: this line looks deceptively like
a line in your code, except the file name is different.  Your file
is called "./filter_template" and this line is from a file named
"/usr/lib/python3/dist-packages/checkbox/lib/template.py".

At line 77 in your code, your calling the "load_file" method on an
instance of a class defined in that file.  Your description sounds
as if you meant to call the "load_file" method on an instance of
your own class.  In other words, it sounds like you're instantiating
the wrong class.

I can't say for certain, because you've left out that bit of your code.


> It's telling me that buffer_new is a str object.
> 
> so if I remove the decode():
> 
> Traceback (most recent call last):
>   File "./run_templates", line 142, in 
> sys.exit(main(sys.argv[1:]))
>   File "./run_templates", line 137, in main
> runner.process(args, options.shell)
>   File "./run_templates", line 39, in process
> records = self.process_output(process.stdout)
>   File "./run_templates", line 88, in process_output
> return template.load_file(output)
>   File "/usr/lib/python3/dist-packages/checkbo

Re: Executing Python Scripts on Mac using Python Launcher

2012-06-26 Thread Hans Mulder
On 26/06/12 20:11:51, David Thomas wrote:
> On Monday, June 25, 2012 7:19:54 PM UTC+1, David Thomas wrote:
>> Hello,
>> This is my first post so go easy on me.  I am just beginning to program 
>> using Python on Mac.  When I try to execute a file using Python Launcher my 
>> code seems to cause an error in terminal, when I execute the exact same 
>> piece of code and run it in windows it seems to execute as exactly intended.
>>  How can I make my python script to open up correctly using Python Launcher? 
>>  I am running 10.7 on my Mac.  I would upload a photo with the error I get 
>> but I can't seem to find the upload feature in this group.
>>
>> Thanks
> 
> Thank you for the fast response.  How can I tell if Python is in the same 
> path as Terminal?

It isn't.  Terminal is an application and is located in
/Applications/Utilities .  Python is an executable, and is
typically located in a "bin" directory.  To find out where
it is, type

type python

at the shell prompt (that's the first prompt you get if you
open a Terminal window).

Possible answers include:

/usr/bin/python
This is the Python shipped by Apple

/Library/Frameworks/Python.framework/Versions/2.7/bin/python
This is a Python from python.org

/opt/local/bin/python
This is a Python from macports

> Is this located in the /usr folder?

If you mean /usr/bin, that's the Python that came with your Mac.
If you haven't installed any other Pythons, then yes, you must
be using that one.


Hope this helps,

-- HansM


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


Re: Executing Python Scripts on Mac using Python Launcher

2012-06-26 Thread Hans Mulder
On 26/06/12 21:51:41, Dennis Lee Bieber wrote:
> On Tue, 26 Jun 2012 10:19:45 -0700 (PDT), David Thomas
>  declaimed the following in gmane.comp.python.general:
> 
> 
>> http://www.freeimagehosting.net/ilbqt
> 
>   That's an interesting configuration...
> 
>   "pythonw.exe" is a version of the Python interpreter designed to NOT
> OPEN a console -- so you've got a configuration saying "open a console
> to run a no-console interpreter".

That's on Windows; on the Mac python and pythonw are identical.
In fact, they're hard links to the same file.

>   Normally pythonw.exe is used with scripts having .pyw extension;
> these are scripts that use tkinter, wxPython, or other GUI system to
> create a graphical environment and don't want a console (terminal)
> window cluttering the screen when they don't use text I/O.
> 
>   Console based programs (.py) should be run using python.exe; adjust
> your settings.

That shouldn't matter on a Mac.

You may want to check "allow #! to override", though.

I mean, if there is a #! in a file pointing to a specific version of
python, then it's probably there for a reason.  For example, the script
might use a third party module installed only in that Python install.

>> http://www.freeimagehosting.net/r5ars
> 
>   And this is to be expected... In Python 2.x, "input()" attempts to
> evaluate the input data, and you didn't supply anything -- hence EOF.
> For your usage, you want "raw_input()", which just grabs the next line
> of text and returns it as a string.

What he says.

>   As for how to run if you've opened a console (shell [bash]) window,
> the way to run a script is to type
> 
>   python .py 


Hope this helps,

-- HansM



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


Re: Executing Python Scripts on Mac using Python Launcher

2012-06-26 Thread Hans Mulder
On 26/06/12 22:41:59, Dave Angel wrote:
> On 06/26/2012 03:16 PM, Hans Mulder wrote:
>> 
>>
>>  Python is an executable, and is
>> typically located in a "bin" directory.  To find out where
>> it is, type
>>
>> type python
>>
>> at the shell prompt (that's the first prompt you get if you
>> open a Terminal window).
>>
>>
> 
> That's a typo.  You presumably meant:
> 
> which python
> 

No, I meant:

$ type python
python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python

'type' is a bash builtin that tells you how bash would interpret
a command.  'which' is a separate program, which tells you how
csh would interpret a command.  For a 'bash' user, 'type' is more
accurate than 'which'.  For example, 'type' recognizes 'bash'
builtins.

-- HansM


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


Re: Executing Python Scripts on Mac using Python Launcher

2012-06-27 Thread Hans Mulder
On 27/06/12 19:05:44, David Thomas wrote:
> Is this why I keep getting an error using launcher?

No.

Yesterday your problem was that you tried this:

input("\n\nPress the enter key to exit")

That works fine in Pyhton3, but you are using python2
and in python2, the you must do this instead:

raw_input("\n\nPress the enter key to exit")

If you still get that error, you can either use "raw_input()"
or switch to Python3, where "input" would be correct.

If you're learning Python from a tutorial, you should
carefully read the first section of the tutorial, where
they mention whether the explains pyhton2 or python3,
and use a matching Python (or a matching tutorial).

If the tutorial doesn't say for which version it is,
then it's for pyhton2 (and several years old).

> Also to open the script in terminal do I need to put
> the following at the beginning of my script:
>
> #!/bin/python

No, that doesn't help.

There are at least three pythons on your system, but
there isn't one in /bin.  There are basically two ways
to run a python script in terminal:

Method 1: type the word "python", followed by a space and
the full path to your script, enclosed in quotes, e.g.

python '/Users/dthomaw86/Documents/Python Documents/game_over_enter_key.py'

The quotes are necessary, because you have a space
character in the path.

Method 2:
Step 1: put valid a '#!' line at the top of the script,
for example:

#!/ust/local/bin/python

Step 2: in Terminal, go to the folder where the script is:

cd '/Users/dthomaw86/Documents/Python Documents'

Step 3: make the script "executable"

chmod +x game_over_enter_key.py

Step 4: you can now run the script with:

./game_over_enter_key.py

If you want to run the script again, you only need to repeat
step 4.  Or use the "up arrow" key in Terminal.

I think you should pick one method and stick with it, until
you get the hang of Terminal.

> On python.org it states to To run your script from the Terminal
> window you must make sure that /usr/local/bin is in your shell
> search path.

The installer for Mac from python.org has installed python2.7
in /Libary/Frameworks/Python.frameworks/Versions/2.7/bin and
added that directory to you shell search path.

> How can I make sure that the Python I have installed on my Mac
> is in my shell search path.
> 
> http://www.freeimagehosting.net/saskk

If you look at the screen shot, you'll see that the shell
has found python in
/Libary/Frameworks/Python.frameworks/Versions/2.7/bin

Incidentally, you don't have to upload screenshots from
Termimal.  You can you just select text in Terminal and
use cmd-C to copy it to the clipboard, and then cmd-V to
paste it to this forum.  This is a text-only forum, but
text from Terminal _is_ text, so that's allowed.

For example:

HansM 4$ cat game_over_enter_key.py
raw_input("\n\nPress the enter key to exit")
HansM 5$ python game_over_enter_key.py


Press the enter key to exit
HansM 6$

> Thanks again and I am sorry for all the questions,
> I am just getting started on Python

That's all right: most people run into this sort of issues
when they try their first script.  There are usually several
ways to do things, and that leads to you getting conflicting
advice.


Hope this helps,

-- HansM


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


Re: Executing Python Scripts on Mac using Python Launcher

2012-06-27 Thread Hans Mulder
On 27/06/12 22:45:47, David Thomas wrote:
> Thank you ever so much raw_input works fine.

> Do you think I should stick with Python 2 before I go to 3?

I think so.  The differences are not that big, but big
enough to confuse a beginner.  Once you know pyhton2,
read http://docs.python.org/py3k/whatsnew/3.0.html
That's the official overview of what has changed between
python2 and python3.

> I have a text book which is using 3 but I've been using an
> online tutorial which has been helping me lots, which uses
> version 2.

If you decide to use the book, you'll want to install Python3
on your Mac.  If you do that, you'll find that typing "python"
in Terminal still gets you python2.7; you have to type "pyhton3"
to get python3.

> I found by just typing python then having a space and
> dragging the file from finder into terminal works.

I just tried it, and it works.  Terminal uses backslashes
to protect characters that need quoting in the shell.  I'd
use single quotes myself, but this also works.

-- HansM

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


Re: how can I implement "cd" like shell in Python?

2012-06-28 Thread Hans Mulder
On 28/06/12 13:09:14, Sergi Pasoev wrote:
> Do you mean to implement the cd command ? To what extent do you want to
> implement it ? if what you want is just to have a script to change the
> current working directory, it is as easy as this:
> 
> 
> import sys
> import os
> os.chdir(sys.argv[1])
> 
> plus you could add some error-handling code.

'cd' is a shell built-in, because otherwise it would have no effect.

You can write a Python script that invokes os.chdir(), but that won't
have any effect on the shell that starts the script.

What are you trying to achieve?

-- HansM

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


Re: 2 + 2 = 5

2012-07-05 Thread Hans Mulder
On 5/07/12 07:32:48, Steven D'Aprano wrote:
> On Wed, 04 Jul 2012 23:38:17 -0400, Terry Reedy wrote:
> 
>> If I run the script in 3.3 Idle, I get the same output you got. If I
>> then enter '5-2' interactively, I still get 3. Maybe the constant folder
>> is always on now.
> 
> Yes, I believe constant folding is always on, since Python 2.4 if I 
> remember correctly. Somebody who cares more than me can possibly check 
> the "What's New" documents :)

It's not a difference between 2.4 and 3.3; the difference is between
Idle and the command-line version of the interactive interpreter.

If I type the same code into Idle and the interactive interpreter
(both using 3.3alpha1), I get 3 in Idle and 2 in Terminal.

I don't quite understand why this difference exists.


Confused,

-- HansM

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


Re: Creating an instance when the argument is already an instance.

2012-07-05 Thread Hans Mulder
On 5/07/12 12:47:52, Chris Angelico wrote:
> On Thu, Jul 5, 2012 at 8:29 PM, Olive  wrote:
>> I am creating a new class: package (to analyse the packages database in
>> some linux distros). I have created a class package such that
>> package("string") give me an instance of package if string is a correct
>> representation of a package. I would like that if pack is already an
>> instance of package then package(pack) just return pack.
> 
> One way would be to make the name "package" actually a wrapper
> function, not the class itself:
> 
 class _package:
>   def __init__(self,arg):
>   # blah blah
>   self.asdf=arg
> 
 def package(arg):
>   if isinstance(arg,_package): return arg
>   return _package(arg)
> 
 a=package("Test")
 b=package(a)
 a is b
> True
> 
> The leading underscore is a common convention meaning "private
> implementation detail".

I think using a factory function is the right idea, but the
code above doesn't solve the problem as stated.  Olive needs
a factory function that takes a string argument and returns
a _package object.

Maybe:

class _package:
def __init__(self, name):
self.name = name
 # etc.

packages = dict()

def package(name):
if name not in packages:
packages[name] = _package(name)
return packages[name]


Hope this helps,

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


Re: 2 + 2 = 5

2012-07-05 Thread Hans Mulder
On 5/07/12 19:03:57, Alexander Blinne wrote:
> On 05.07.2012 16:34, Laszlo Nagy wrote:
> five.contents[five.contents[:].index(5)] = 4
> 5
>> 4
> 5 is 4
>> True

> That's surprising, because even after changing 5 to 4 both objects still
> have different id()s (tested on Py2.7), so 5 is 4 /should/ still be
> False (But isn't on my 2.7). But that's some implementation detail we
> are not supposed to play with ;)

On my 2.7, id(5) gives the same value as id(4) == id(2+2), but id(2+3)
has a different value.  The 'is' operator is consistent with 'id':

>>> 4 is 5
True
>>> 2+2 is 2+3
False

This is when using the interactive interpreter; it may be different
in Idle.

-- HansM


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


Re: Confusing datetime.datetime

2012-07-06 Thread Hans Mulder
On 6/07/12 00:55:48, Damjan wrote:
> On 05.07.2012 16:10, Damjan wrote:
>> I've been struggling with an app that uses
>> Postgresql/Psycopg2/SQLAlchemy  and I've come to this confusing
>> behaviour of datetime.datetime.
> 
> 
> Also this:
> 
> #! /usr/bin/python2
> # retardations in python's datetime
> 
> import pytz
> TZ = pytz.timezone('Europe/Skopje')
> 
> from datetime import datetime
> 
> x1 = datetime.now(tz=TZ)
> x2 = datetime(x1.year, x1.month, x1.day, tzinfo=TZ)
> 
> assert x1.tzinfo == x2.tzinfo
> 
> WHY does the assert throw an error???

Because x1 and x2 have different time zones.

The tzinfo field in x2 is equal to TZ and has a UTC offset of 1 hour.
The tzinfo field in x1 contains the DST version of that timezone,
with a UTC offset of 2 hours, because Skopje is currently on DST.

I think you want:

x2 = TZ.localize(datetime(x1.year, x1.month, x1.day))

That produces a datetime with the year, month and day set as indicated
and tzinfo set to the correct UTC offset for that date, at 00:00 hours.

Or maybe you need:

x2 = TZ.localize(datetime(x1.year, x1.month, x1.day, 12))
x2 = x2.replace(hour=0)

That determines whether DST should be on at noon, and then resets the
hour field to zero.  This produces the same outcome as the one liner,
except on days when DST is switched on or off.


Hope this helps,

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


Re: git_revision issues with scipy/numpy/matplotlib

2012-07-07 Thread Hans Mulder
On 7/07/12 07:47:56, Stephen Webb wrote:
> I installed py27-numpy / scipy / matplotlib using macports, and it ran 
> without failing.
> 
> When I run Python I get the following error:
> 
> $>> which python
> 
> /Library/Frameworks/Python.framework/Versions/2.7/bin/python

That's a python from python.org, not from MacPorts.

> $>> python
> 
> Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
 import numpy
> Traceback (most recent call last):
>   File "", line 1, in 
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py",
>  line 128, in 
> from version import git_revision as __git_revision__
> ImportError: cannot import name git_revision
> 
> I get the same error for all three packages. Is this a MacPorts issue or a 
> different issue?

You have (at least) three pythons on your Mac: one from Apple
in /usr/bin, one from python.org (the one you found) and one
from MacPorts in /opt/local/bin/python

You may want to add /opt/local/bin to the front of your PATH;
that will also pick up the MacPorts version of other software
you've installed from MacPorts.

Or you can explicitly type the full path of the python you want.

Or you can define aliases, for example:

alias apple_python=/usr/bin/python
alias macport_python=/opt/local/bin/python

lfpv=/Library/Frameworks/Python.framework/Versions
alias python_org_python=$lfpv/2.7/bin/python


Hope this helps,

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


Re: git_revision issues with scipy/numpy/matplotlib

2012-07-07 Thread Hans Mulder
On 7/07/12 14:09:56, Ousmane Wilane wrote:
>>>>>> "H" == Hans Mulder  writes:
> 
> H> Or you can explicitly type the full path of the python you want.
> 
> H> Or you can define aliases, for example:
> 
> H> alias apple_python=/usr/bin/python alias
> H> macport_python=/opt/local/bin/python
> 
> H> lfpv=/Library/Frameworks/Python.framework/Versions alias
> H> python_org_python=$lfpv/2.7/bin/python
> 
> 
> Or alternatively use `port select --set' to make one of the MacPort version 
> the
> default:
> 
> imac:~ wilane$ port select --list python
> Available versions for python:
>   none
>   python25-apple
>   python26
>   python26-apple
>   python27 (active)
>   python27-apple
>   python32

That would work if the OP had /opt/local/bin early in his searcht path.
However, the OP has installed Python27 from python.org, and that has
prepended /Library/Frameworks/Python.framework/Versions/2.7/bin to
his PATH, overriding anything he does with "port select".

He could, of course, change his PATH and move /opt/local/bin to the
front and then use "port select".

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


Re: lambda in list comprehension acting funny

2012-07-11 Thread Hans Mulder
On 11/07/12 20:38:18, woooee wrote:
> You should not be using lambda in this case
> .for x in [2, 3]:
> .funcs = [x**ctr for ctr in range( 5 )]
> .for p in range(5):
> .print x, funcs[p]
> .print

The list is called "funcs" because it is meant to contain functions.
Your code does not put functions in the list, so it doesn't do what
he wants.

He could do:

funcs = []
for i in range(5):
def f(x):
return x**i
funcs.append(f)
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )

 and it will print 16, 16, 16 for the same reason as the lambda
version.  On the other hand, this does what he wants:

funcs = []
for i in range(5):
def f(x, i=i):
return x**i
funcs.append(f)
print funcs[0]( 2 )
print funcs[1]( 2 )
print funcs[2]( 2 )

The lambda keyword is a red herring.  The question is really about
functions, and how they handle non-local variables.  The good news
is that 'lambda' and 'def' work exactly the same in that regards.
So once you understand the finer points about 'def', you no longer
have a reason to avoid 'lambda'.

Hope this helps,

-- HansM


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


Re: How to safely maintain a status file

2012-07-12 Thread Hans Mulder
On 12/07/12 14:30:41, Laszlo Nagy wrote:
>> You are contradicting yourself. Either the OS is providing a fully
>> atomic rename or it doesn't. All POSIX compatible OS provide an atomic
>> rename functionality that renames the file atomically or fails without
>> loosing the target side. On POSIX OS it doesn't matter if the target
>> exists.

> This is not a contradiction. Although the rename operation is atomic,
> the whole "change status" process is not. It is because there are two
> operations: #1 delete old status file and #2. rename the new status
> file. And because there are two operations, there is still a race
> condition. I see no contradiction here.

On Posix systems, you can avoid the race condition.  The trick is to
skip step #1.  The rename will implicitly delete the old file, and
it will still be atomic.  The whole process now consists of a single
stop, so the whole process is now atomic.

>> You don't need locks or any other fancy stuff. You just need to make
>> sure that you flush the data and metadata correctly to the disk and
>> force a re-write of the directory inode, too. It's a standard pattern on
>> POSIX platforms and well documented in e.g. the maildir RFC.

> It is not entirely true. We are talking about two processes. One is
> reading a file, another one is writting it. They can run at the same
> time, so flushing disk cache forcedly won't help.

On Posix systems, it will work, and be atomic, even if one process is
reading the old status file while another process is writing the new
one.  The old file will be atomically removed from the directory by
the rename operation; it will continue to exists on the hard drive, so
that the reading process can continue reading it.  The old file will
be deleted when the reader closes it.  Or, if the system crashed before
the old file is closed, it will deleted when the system is restarted.

On Windows, things are very different.

Hope this helps,

-- HansM


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


Re: adding a simulation mode

2012-07-13 Thread Hans Mulder
On 13/07/12 04:16:53, Steven D'Aprano wrote:
> On Thu, 12 Jul 2012 16:37:42 +0100, andrea crotti wrote:
> 
>> 2012/7/12 John Gordon :
>>> In  andrea crotti
>>>  writes:
>>>
 Well that's what I thought, but I can't find any explicit exit
 anywhere in shutil, so what's going on there?
>>>
>>> Try catching SystemExit specifically (it doesn't inherit from
>>> Exception, so "except Exception" won't catch it.)
>>>
>>
>> Ah yes that actually works, but I think is quite dodgy, why was it done
>> like this?

It may be that the function you're calling found a problem that the
author thinks is so grave that they shouldn't give you an opportunity
to deal with it.

If that's the case, I would be inclined to think that they are wrong.

> Built-in exceptions SystemExit, KeyboardInterrupt and GeneratorExit 
> deliberately do not inherit from Exception since they are not meant to be 
> caught by "catch-all" try...except Exception clauses.
> 
> You can see the exception hierarchy here:
> 
> http://docs.python.org/library/exceptions.html#exception-hierarchy
> 
> Please do NOT catch BaseException, since that is the wrong thing to do. 

I would agree if you had said "in production code".

If you are investigating why a third-party function is stopping your
interpreter, then catching BaseException may tell you that the code
is raising the wrong kind of Exception.  Once you know what kind the
function is raising, you should catch only that particular excpetion
subclass.


> If you must catch SystemExit, KeyboardInterrupt, etc. they you should do 
> so as separate catch clauses:
> 
> try:
> main()
> except SystemExit as e:
> print(e)  # see if we can find out who is raising this

If you want to find out who is raising the exception, you could
try this:

except SystemExit:
import traceback
traceback.print_exc()

That will print a complete stack trace.

If you only need to know what kind of exception you have,
you can do:

print(repr(e))

A simple print(e) will print str(e), which in the case of
SystemExit, is an empty string.  That's not very informative.

> except KeyboardInterrupt:
> print("Mwahahaha my pretty, you cannot cancel this!!!")
> print("...er, now what do I do?")
> except Exception:
> print("why am I catching exceptions I can't recover from?")


Hope this helps,

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


Re: lambda in list comprehension acting funny

2012-07-13 Thread Hans Mulder
On 13/07/12 18:12:40, Prasad, Ramit wrote:
>> VERBOSE = True
>>
>> def function(arg):
>> if VERBOSE:
>>print("calling function with arg %r" % arg)
>> process(arg)
>>
>> def caller():
>> VERBOSE = False
>> function(1)
>>
>> -
>> Python semantics: function sees VERBOSE False
>> Haskell semantics: function sees VERBOSE True

 def caller():
> ... VERBOSE = False
> ... function(1)
 def function(arg):
> ... if VERBOSE:
> ...print("calling function with arg %r" % arg)
> ... 
 VERBOSE = True
 caller()
> calling function with arg 1
> 
> I might be being OCD, but caller needs `global VERBOSE` for that to
> work as you explain.

That would be quite different from what Rusi is after.

If you add `global VERBOSE` to `caller`, then there is only one
variable named `VERBOSE` and what `function` does, depends on
the most recent assignment to that variable.

If you remove your `global VERBOSE`, then there are two
variables by that name, one global and one local to `caller`.
In that case, there is the question of which one `function`
will use.

The function `function` refers to a variable `VERBOSE` that
isn't local.  In some programming langauages, the interpreter
would then scan the call stack at run-time, looking for a scope
where that name is defined.  It would find the local one in
`caller`.  This is known as "dynamic binding".

Other interpreters use the `VERBOSE` that was in scope at
the point in the program text where `function` was defined.
In this case, that would be the global one.  This is called
"lexical binding".

Some programming languages allow you to indicate on a per-
variable basis whether you want dynamic or lexical binding.

Python is firmly in the lexical camp.  Dynamic binding is not
available in Python, and never will be.


Hope this helps,

-- HansM


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


Re: [Python] RE: How to safely maintain a status file

2012-07-13 Thread Hans Mulder
On 13/07/12 19:59:59, Prasad, Ramit wrote:

> I lean slightly towards the POSIX handling with the addition that 
> any additional write should throw an error. You are now saving to 
> a file that will not exist the moment you close it and that is
> probably not expected.

I'd say: it depends.

If the amount of data your script needs to process does not fit
in RAM, then you may want to write some of it to a temporary file.
On a Posix system, it's entirely normal to unlink() a temp file
first thing after you've created it.  The expectation is that the
file will continue to exists, and be writeable, until you close it.

In fact, there's a function in the standard library named
tempfile.TemporaryFile that does exactly that: create a file
and unlink it immediately.  This function would be useless
if you couldn't write to your temporary file.

Hope this helps,

-- HansM


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


Re: lambda in list comprehension acting funny

2012-07-13 Thread Hans Mulder
On 13/07/12 20:54:02, Ian Kelly wrote:
> I've also seen the distinction described as "early" vs. "late" binding
> on this list, but I'm not sure how precise that is -- I believe that
> terminology more accurately describes whether method and attribute
> names are looked up at compile-time or at run-time, late binding being
> the feature that makes duck typing possible.

I think that these terms describe the problem at the start of
this thread: the OP was expecting early binding.  However, Python
does late binding, or "acts funny" as it says in the subject line.


-- HansM


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


Re: howto do a robust simple cross platform beep

2012-07-14 Thread Hans Mulder
On 14/07/12 20:49:11, Chris Angelico wrote:
> On Sun, Jul 15, 2012 at 3:54 AM, Dieter Maurer  wrote:
>> I, too, would find it useful -- for me (although I do not hate myself).
>>
>> Surely, you know an alarm clock. Usually, it gives an audible signal
>> when it is time to do something. A computer can in principle be used
>> as a flexible alarm clock - but it is not so easy with the audible signal...
>> An audible signal has the advantage (over a visual one) that you can
>> recognize it even when you are not looking at the screen (because you
>> are thinking).
>>
>> Unfortunately, I had to give up. My new computer lacks a working
>> speaker...
> 
> There's a simple cheat you can do. Just invoke some other application
> to produce the sound! My current alarm clock comes in two modes: it
> either picks a random MIDI file from Gilbert and Sullivan's
> "Ruddigore", or it plays the "Alice: Madness Returns" theme; in each
> case it just invokes the file with its default association (see the
> "start" command in Windows, or "gnome-open" in, well, GNOME).
> 
> Of course, working speaker IS a prerequisite.

The other prerequisite is that the use is physically near the
compueter where your Python process is running.

If, for exmple, I'm ssh'ed into my webserver, then sending a sound
file to the server's speaker may startle someone in the data centre,
but it won't attract my attention.  If, OTOH, you do:

print "\7"

, then an ASCII bell will be sent across the network, and my
terminal emulator will beep.

It all depends.


-- HansM


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


Re: lambda in list comprehension acting funny

2012-07-15 Thread Hans Mulder
On 15/07/12 10:44:09, Chris Angelico wrote:
> On Sun, Jul 15, 2012 at 6:32 PM, Steven D'Aprano
>  wrote:
>> At compile time, Python parses the source code and turns it into byte-
>> code. Class and function definitions are executed at run time, the same
>> as any other statement.
> 
> Between the parse step and the 'def' execution, a code object is
> created. When you call it, that code object already exists. Nothing
> else really matters, unless there's a bug in the Python optimizer or
> something weird like that. The nearest thing Python _has_ to a
> "compile time" is the execution of def.
> 
> ChrisA

"Compile time" is the phase when your Python code is turned into byte
code, or a SyntaxError is raised.  In this phase, a "code object" is
created for the function.

"Function definition time" is when the "def" command is executed.
In this phase, default arguments are computed, and a "function object"
is created.  Among the attributes of the function object are the code
object, and "cell" objects containing the bindings for its non-local
variables.  These bindings are used to read the variable's current
value at the time the function uses the variable.


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


Re: Python 2.6 on Mac 10.7 doesn't work

2012-07-17 Thread Hans Mulder
On 17/07/12 15:47:05, Naser Nikandish wrote:
> Hi,
> 
> I am trying to install Python 2.6 on Mac OS Lion. Here is what I did:
> 
> 1- Download Mac Installer disk image (2.6) (sig)   from
>http://www.python.org/getit/releases/2.6/
> 
> 2- Install it
> 
> 3- Run Python Luncher, go to Python Luncher preferences, and change the
>interpreter to Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw
> 
> Now that I want to open IDLE and open my old projects, it doesn't run! 
> Is there anything that I am missing?

You may be missing a leading slash.  The path to pythonw is
/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw


> oh btw, I used to use Python 2.6.7 that comes preinstalled on Mac OS.

That could also be the problem.  You could try using the Python you've
downloaded in step 1.

> I really appreciate your help.

You could try opening /Applications/Utilities/Terminal.app and running:

/Library/Frameworks/Python.framework/Versions/2.6/bin/pyt\
hon /Applications/Python\ 2.6/IDLE.app/Contents/MacOS/IDLE

(you can type it one two lines, as shown, or as a single rather long
line, leaving out the backslash-newline).

That should run Idle, or, if it doesn't, it should give you a useful
error message.

Hope this helps,

-- HansM

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


Re: Odd csv column-name truncation with only one column

2012-07-19 Thread Hans Mulder
On 19/07/12 13:21:58, Tim Chase wrote:
> tim@laptop:~/tmp$ python
> Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 import csv
 from cStringIO import StringIO
 s = StringIO('Email\n...@example.com\n...@example.org\n')
 s.seek(0)
 d = csv.Sniffer().sniff(s.read())
 s.seek(0)
 r = csv.DictReader(s, dialect=d)
 r.fieldnames
> ['Emai', '']
> 
> I get the same results using Python 3.1.3 (also readily available on
> Debian Stable), as well as working directly on a file rather than a
> StringIO.
> 
> Any reason I'm getting ['Emai', ''] (note the missing ell) instead
> of ['Email'] as my resulting fieldnames?  Did I miss something in
> the docs?

The sniffer tries to guess the column separator.  If none of the
usual suspects seems to work, it tries to find a character that
occurs with the same frequency in every row.  In your sample,
the letter 'l' occurs exactly once on each line, so it is the
most plausible separator, or so the Sniffer thinks.

Perhaps it should be documented that the Sniffer doesn't work
on single-column data.

If you really need to read a one-column csv file, you'll have
to find some other way to produce a Dialect object.  Perhaps the
predefined 'cvs.excel' dialect matches your data.  If not, the
easiest way might be to manually define a csv.Dialect subclass.

Hope this helps,

-- HansM

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


Re: Odd csv column-name truncation with only one column

2012-07-20 Thread Hans Mulder
On 19/07/12 23:10:04, Dennis Lee Bieber wrote:
> On Thu, 19 Jul 2012 13:01:37 -0500, Tim Chase
>  declaimed the following in
> gmane.comp.python.general:
> 
>>  It just seems unfortunate that the sniffer would ever consider
>> [a-zA-Z0-9] as a valid delimiter.

+1

>   I'd suspect the sniffer logic does not do any special casing
> -- any /byte value/ is a candidate for the delimiter.

The sniffer prefers [',', '\t', ';', ' ', ':'] (in that order).
If none of those is found, it goes to the other extreme and considers
all characters equally likely.

> This would allow for usage of some old ASCII control characters --
> things like  x1F (unit separator)

If the Sniffer excludes [a-zA-Z0-9] (or all alphanumerics) as
potential delimiters, than control characters such as "\x1F" are
still possible.

> {Next is to rig the sniffer to identify x1F for fields, and x1E
> for records }

The sniffer will always guess '\r\n' as the line terminator.

That should not stop you from creating a dialect with '\x1E' as
the line terminator.  Just don't expect the sniffer to recognize
that dialect.

-- HansM


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


Re: Encapsulation, inheritance and polymorphism

2012-07-20 Thread Hans Mulder
On 20/07/12 11:05:09, Virgil Stokes wrote:
> On 20-Jul-2012 10:27, Steven D'Aprano wrote:
>> On Fri, 20 Jul 2012 08:20:57 +1000, Chris Angelico wrote:
>>
>Since the current evidence indicates the universe will just
> keep
> expanding, it's more of a "deep freeze death..."
 Heat death means *lack* of heat.
>>> The second law of thermodynamics states that energy tends to go from
>>> higher states to lower, with heat being the very lowest. It's possible
>>> to do work using (say) kinetic energy, and in the process, some of that
>>> energy becomes heat. It's also possible to do work with any difference
>>> in temperature (eg Stirling engines), so the state of the universe in
>>> which it's no longer possible to do any work will be one in which all
>>> energy is heat and everything's at the same temperature. That doesn't
>>> mean a lack of heat; in fact, it implies that there'll be rather more
>>> heat than there now is, because we currently have a whole lot of
>>> chemical energy available to be used.
>> Yes, but the point is, that heat will be *incredibly* diffuse,
>> essentially spread over the entire universe, which will be MUCH bigger
>> than it is now, and hence the temperature will be low even though the
>> total amount of heat will be high.
>>
>> The average temperature of the universe now is about 2.7 degrees above
>> absolute zero (i.e. 2.7 K, -270.45 C or -454.81 F), with individual
>> hotspots reaching into millions of degrees or higher. By the time the
>> last of the stars burn out, the average temperature will be a minuscule
>> fraction of a degree above absolute zero, and the only hotspots will be
>> the slowly cooling neutron stars.
>>
>>
>>> But in any case, that's a long way off...
>> I once went to an astronomy lecture where the lecturer was talking about
>> the eventual death of the sun. He said, "In about 10 billion years, the
>> sun will consume almost all of its fuel. It will cool and expand into a
>> red giant, and the earth will be engulfed by the expanded sun and
>> destroyed."
>>
>> This fellow sitting next to me got all agitated, stood up and cried out,
>> "Does the government know about this? We have to do something!"
>>
>> The lecturer said "Don't worry sir, there's no need to panic, this won't
>> happen for billions of years."
>>
>> The fellow looked relived and said "Oh thank god, I thought you said
>> *million*!"
>>
> How does this relate to the python list?

This thread is as coherent as a typical episode of
Monty Python's Flying Circus :-)


-- HansM

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


Re: no data exclution and unique combination.

2012-08-10 Thread Hans Mulder
On 9/08/12 23:33:58, Terry Reedy wrote:
> On 8/9/2012 4:06 PM, giuseppe.amatu...@gmail.com wrote:
[...]
>> unique=dict()
>> for row in  array2D :
>>  row = tuple(row)
>>  if row in unique:
>>  unique[row] += 1
>>  else:
>>  unique[row] = 1
> 
> I believe the 4 lines above are equivalent to
>unique[row] = unique.get(row, 0) + 1


I would write that bit as:

from collections import defaultdict

unique = defaultdict(int)
for row in  array2D:
unique[row] += 1


Hope this helps,

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


Re: [c-api]Transmutation of an extension object into a read-only buffer adding an integer in-place.

2012-08-10 Thread Hans Mulder
On 10/08/12 10:20:00, Giacomo Alzetta wrote:
> I'm trying to implement a c-extension which defines a new class(ModPolynomial 
> on the python side, ModPoly on the C-side).
> At the moment I'm writing the in-place addition, but I get a *really* strange 
> behaviour.
> 
> Here's the code for the in-place addition:
> 
> #define ModPoly_Check(v) (PyObject_TypeCheck(v, &ModPolyType))
> [...]
> static PyObject *
> ModPoly_InPlaceAdd(PyObject *self, PyObject *other)
> {
> 
> if (!ModPoly_Check(self)) {
> // This should never occur for in-place addition, am I correct?
> if (!ModPoly_Check(other)) {
> PyErr_SetString(PyExc_TypeError, "Neither argument is a 
> ModPolynomial.");
> return NULL;
> }
> return ModPoly_InPlaceAdd(other, self);
> } else {
> if (!PyInt_Check(other) && !PyLong_Check(other)) {
> Py_INCREF(Py_NotImplemented);
> return Py_NotImplemented;
> }
> }
> 
> ModPoly *Tself = (ModPoly *)self;
> PyObject *tmp, *tmp2;
> tmp = PyNumber_Add(Tself->ob_item[0], other);
> tmp2 = PyNumber_Remainder(tmp, Tself->n_modulus);
> 
> Py_DECREF(tmp);
> tmp = Tself->ob_item[0];
> Tself->ob_item[0] = tmp2;
> Py_DECREF(tmp);
> 
> printf("%d\n", (int)ModPoly_Check(self));
> return self;
> 
> }

I have no experience writing extensions in C, but as I see it,
you're returning a new reference to self, so you'd need:

Py_INCREF(self);

If you don't, then a Py_DECREF inside the assignment operator
causes your polynomial to be garbage collected.  Its heap slot
is later used for the unrelated buffer object you're seeing.

Hope this helps,

-- HansM

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


Re: [c-api]Transmutation of an extension object into a read-only buffer adding an integer in-place.

2012-08-10 Thread Hans Mulder
On 10/08/12 11:25:36, Giacomo Alzetta wrote:
> Il giorno venerdì 10 agosto 2012 11:22:13 UTC+2, Hans Mulder ha scritto:
[...]
> Yes, you're right. I didn't thought the combined operator would do a Py_DECREF
> if the iadd operation was implemented, but it obviosuly makes sense.

The += operator cannot know if the iadd returns self or a newly created
object.  Mutable types usually do the former; non-mutable types must do
the latter.

Come to think of it: why are your polynomials mutable?

As a mathematician, I would think of polynomials as elements of
some kind of ring, and I'd expect them to be non-mutable.

-- HansM

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


Re: Unable to execute the script

2012-08-11 Thread Hans Mulder
On 11/08/12 00:48:38, Dennis Lee Bieber wrote:
> On Fri, 10 Aug 2012 12:35:06 -0700, Smaran Harihar
>  declaimed the following in
> gmane.comp.python.general:
> 
>> Hi Tim,
>>
>> this is the output for the ls -lsF filename
>>
>> 8 -rwxr-xr-x 1 root root 5227 Jul 30 13:54 iplantgeo_cgi.py*
>>
>   
> 
>   A CGI script owned by root?

Why not?

It's not setuid, so being owned by root does not give it
any special privileges.

> What "user" does your web server run as?
> I'd recommend setting that user as the owner of the CGI script.

That's definitely a bad idea.  More so if it's writeable by its
owner, as is the case here.  It would mean that if a security
hole allows intruders to write to arbitrary files, then they
can overwrite this script and that would allow them to execute
arbitrary code.

-- HansM

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


Re: [Newbie] How to wait for asyncronous input

2012-08-11 Thread Hans Mulder
On 11/08/12 09:07:51, pozz wrote:
> Il 11/08/2012 01:12, Dennis Lee Bieber ha scritto:
>> What you apparently missed is that serial.read() BLOCKs until data
>> is available (unless the port was opened with a read timeout set).
>> [...]
>>
>> serial.read() may, there for, be using select() behind the scenes.
> 
> Hmm..., so I could open the serial port with timeout=0 so the read(),
> that runs in a different thread, would block forever, so putting the
> thread in a sleep state until some bytes arrive.
> 
> When the main thread wants to close the serial port, the receiving
> thread can be killed


How would you do that?

IFAIK, there is no way in Python to kill a thread.


The usual work-around is to have a global flag that you'd set
to True when the main thread wants to close the port.  The read
would have a timeout of 1 second of so, so that the reading
thread is woken up every second, so it can check that flag and
wind up when the flag is set.


Hope this helps,

-- HansM

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


Re: Arithmetic with Boolean values

2012-08-14 Thread Hans Mulder
On 12/08/12 22:13:20, Alister wrote:
> On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote:
> 
>> On 12/08/2012 17:59, Paul Rubin wrote:
 which can be simplified to:
 for x in range(len(L)//2 + len(L)%2):
>>>
>>> for x in range(sum(divmod(len(L), 2))): ...
>>>
>>>
>> So who's going to be first in with "and thou shalt not count to 4..."?
> 
> Five is right out

Neither count thou two, excepting that thou then proceed to three

-- HansM




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


Re: _mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host

2012-08-15 Thread Hans Mulder
On 15/08/12 15:30:26, nepaul wrote:
> The code:
> import MySQLDB
> strCmd = "user = 'root', passwd = '123456', db = 'test', host = 'localhost'"
> 
> 
> 
> _mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host 'user = 
> 'root',
> passwd = '123456', db = 'test', host = 'localhost'' (11004)")

This message means that the MySQL connector cannot find 'localhost'.
That's odd, since localhost should be your own computer.

Which OS are you using?  Is TCP/IP installed and enabled?

Can you find an entry for 'localhost' in your /etc/hosts file?
Can you ping it?

What happens if you try host='127.0.0.1'?

Incidentally, connecting as 'root' for non-administrative purposes is
considered bad practice.  Consider creating a 'test' account that only
has access to the 'test' database.

Oh, and I hope that '123456' is not really the password for 'root'.


Hope this helps,

-- HansM

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


Re: dbf.py API question concerning Index.index_search()

2012-08-16 Thread Hans Mulder
On 16/08/12 01:26:09, Ethan Furman wrote:
> Indexes have a new method (rebirth of an old one, really):
> 
>   .index_search(
>  match,
>  start=None,
>  stop=None,
>  nearest=False,
>  partial=False )
> 
> The defaults are to search the entire index for exact matches and raise
> NotFoundError if it can't find anything.
> 
> match is the search criteria
> start and stop is the range to search in
> nearest returns where the match should be instead of raising an error
> partial will find partial matches
> 
> The question is what should the return value be?
> 
> I don't like the usual pattern of -1 meaning not found (as in
> 'nothere'.find('a')), so I thought a fun and interesting way would be to
> subclass long and override the __nonzero__ method to return True/False
> based on whether the (partial) match was found.  The main problems I see
> here is that the special return value reverts to a normal int/long if
> anything is done to it (adding, subtracting, etc), and the found status
> is lost.
> 
> The other option is returning a (number, bool) tuple -- safer, yet more
> boring... ;)

I think you should go for the safe boring option, because in many use
cases the caller will need to known whether the number you're returning
is the index of a match or just the nearest non-match.  The caller could
redo the match to find out.  But you have already done the match, so you
might as well tell them the result.


Hope this helps,

-- HansM

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


type(None)()

2012-08-16 Thread Hans Mulder
On 8/08/12 04:14:01, Steven D'Aprano wrote:
> NoneType raises an error if you try to create a second instance. bool
> just returns one of the two singletons (doubletons?) again.
>
> py> type(None)()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: cannot create 'NoneType' instances

Why is that?

Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?

> py> type(False)() is False
> True


Just wondering,

-- HansM

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


Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Hans Mulder
On 16/08/12 14:52:30, Thomas Bach wrote:
> On Thu, Aug 16, 2012 at 12:16:03AM +, Steven D'Aprano wrote:
>> > Some comments:
>> > 
>> > 1) What you show are not "use cases", but "examples". A use-case is a 
>> > description of an actual real-world problem that needs to be solved. A 
>> > couple of asserts is not a use-case.
> Thanks for the clarification on that one. So, here's the use-case: I'm
> querying the crunchbase API which returns JSON data and is rather
> poorly documented. I want to create a data model for the companies
> listed on Crunchbase in order to be able to put the queried data in a
> data-base. As I am too lazy to examine all the data by hand I thought
> I automatize this. I thought that it would be nice to be able to pass
> a function a parsed JSON object (AFAIK these are lists, dicts,
> strings, ints, floats, strs in Python) and it returns me the type of
> these objects. For the simple classes (str, int, float) this is quite
> trivial: F('foo') should return `str' and F(8) should return `int'.
> 
> For a compound object like dict I would like it to return the data
> fields with their type. Hence, F({'foo': 8}) should return 
> {'foo': int}, and given that f = F({'foo': {'bar': 80}}) I would like
> f to equal to {'foo': dict}, with the option to query the type of
> 'foo' via f.foo, where the latter should equal to {'bar': int}. So
> far, this is not a complicated case. But, sometimes a data field on
> returned data set is simply None. Thus, I want to extract the types from
> another data set and merge the two.
> 
> So, my question (as far as I can see it, please correct me if I am
> wrong) is less of the "How do I achieve this?"-kind, but more of the
> "What is a clean design for this?"-kind. My intuitive thought was that
> the `merge' function should be a part of the object returned from `F'.

The misunderstanding is that you feel F should return an object with
a 'merge' method and a varying abse type, while Steven and others
think that F should be a function.

Maybe something like:

def F(obj):
if obj is None:
return None
tp = type(obj)
if tp in (bool, int, float, str):
return tp
elif tp is list:
return merge([F(elem) for elem in obj])
elif tp is dict:
return dict((k, F(v)) for k,v in obj.iteritems())
else:
raise ValueError("Unexpected type %s for value %s" %(tp, obj))

def merge(lst):
if None in lst:
not_nones = [elem for elem in lst if elem is not None]
if not_nones:
not_none = not_nones[0]
lst = [not_none if elem is None else elem for elem in lst]
else:
return lst  # all elements are None; nothing can be done
types = {}
for elem in lst:
if type(elem) is dict:
for k,v in elem.iteritems():
if v is None:
if k in types:
elem[k] = types[k]
else:
for other in lst:
if (other is not elem
and type(other) is dict
and k in other
and other[k] is not None
):
elem[k] = types[k] = other[k]
break
return lst


The merge logic you have in mind may be different from what I just
made up, but the idea remains: F and merge can be functions.


Hope this helps,

-- HansM



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


Re: [ANNC] pybotwar-0.8

2012-08-17 Thread Hans Mulder
On 16/08/12 23:34:25, Walter Hurry wrote:
> On Thu, 16 Aug 2012 17:20:29 -0400, Terry Reedy wrote:
> 
>> On 8/16/2012 11:40 AM, Ramchandra Apte wrote:
>>
>>> Look you are the only person complaining about top-posting.
>>
>> No he is not. Recheck all the the responses.
>>
>>> GMail uses top-posting by default.
>>
>> It only works if everyone does it.
>>
>>> I can't help it if you feel irritated by it.
>>
>> Your out-of-context comments are harder to understand. I mostly do not
>> read them.
> 
> It's strange, but I don't even *see* his contributions (I am using a 
> regular newsreader - on comp.lang.python - and I don't have him in the 
> bozo bin). It doesn't sound as though I'm missing much.

I don't see him either.  That is to say: my ISP doesn't have his
posts in comp.lang.python,  The group gmane.comp.python.general
on Gname has them. so if you're really curious, you can point
your NNTP client at news.gmane.org.

> But I'm just curious. Any idea why that would be the case?

Maybe there's some kind of filer in the mail->usenet gateway?

HTH,

-- HansM



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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-20 Thread Hans Mulder
On 20/08/12 14:36:58, Guillaume Comte wrote:
> In fact, socket.create_connection is for TCP only so I cannot use it for a 
> ping implementation.

Why are you trying to reimplement ping?

All OS'es I am aware of come with a working ping implementation.


> Does anyone have an idea about how to be able to set a source address for 
> ICMP messages?

Did you try not setting it?

The default is probably your own IP address, which is the only
sensible value anyway.  Or are you trying to cause confusion
by sending ICMP packets with a forged source address?

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


Re: [CGI] Basic newbie error or server configuration error?

2012-08-20 Thread Hans Mulder
On 20/08/12 15:50:43, Gilles wrote:
> On Mon, 20 Aug 2012 07:59:39 -0400, Rod Person
>  wrote:
>> Check the Apache error log, there should be more information there.
> 
> It's a shared account, so I only have access to what's in cPanel,
> which didn't display anything.

Most such panels have a button to show the error log for your own site.

If you can't find it, ask the help desk of the web hosting company.

If there really is no way for you to see the error log, ask the help
desk to mail you the error message.

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Hans Mulder
On 22/08/12 08:21:47, Santosh Kumar wrote:
> Here is the script I am using:
> 
> from os import linesep
> from string import punctuation
> from sys import argv
> 
> script, givenfile = argv
> 
> with open(givenfile) as file:
> # List to store the capitalised lines.
> lines = []
> for line in file:
> # Split words by spaces.
> words = line.split(' ')
> for i, word in enumerate(words):
> if len(word.strip(punctuation)) > 3:
> # Capitalise and replace words longer than 3 (without
> punctuation)
> words[i] = word.capitalize()
> # Join the capitalised words with spaces.
> lines.append(' '.join(words))
> # Join the capitalised lines by the line separator
> capitalised = linesep.join(lines)
> # Optionally, write the capitalised words back to the file.
> 
> print(capitalised)
> 
> 
> Purpose of the script:
> To capitalize the first letter of any word in a given file, leaving
> words which have 3 or less letters.
> 
> Bugs:
> I know it has many bugs or/and it can be improved by cutting down the
> code, but my current focus is to fix this bug:
>   1. When I pass it any file, it does it stuff but inserts a blank
> line every time it processes a new line. (Please notice that I don't
> want the output in an another file, I want it on screen).

The lines you read from your input file end in a line separator.
When you print them, the 'print' command adds another line separator.
This results in two line separators in a row, in other words, a blank
line.

The best way to solve this is usually to remove the line separator
right after you've read in the line.  You could do that by inserting
after line 10:

line = line.rstrip()

That will remove all whitespace characters (spaces, tabs, carriage
returns, newlines) from the end of the line.

Alternatively, if you want to remove only the line separator,
you could do:

if line.endswith(linesep):
line = line[:-len(linesep)]

The 'if' command is only necessary for the last line, which may or
may not end in a linesep.  All earlier lines are guaranteed to end
with a linesep.


Hope this helps,

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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-22 Thread Hans Mulder
On 22/08/12 09:29:37, Guillaume Comte wrote:
> Le mercredi 22 août 2012 04:10:43 UTC+2, Dennis Lee Bieber a écrit :
>> On Tue, 21 Aug 2012 10:00:28 -0700 (PDT), Guillaume Comte
>>  declaimed the following in
>> gmane.comp.python.general:

>>  A later follow-up

>>> Unfortunately, my_socket.bind((src_addr, 1)) doesn't work. I get the
>>> error message: "socket.error: [Errno 49] Can't assign requested address"...

>>  Since .bind() is used to set up a /listener/, the network stack

Not necessarily.  That is, a listener is normally  bound to a
specific port, since otherwise the client doesn't know what port
to connect to (unless you provide that factoid on another port).

But nothing prevents a client from binding its socket to a
specific port number.  These days, that doesn't buy you much,
but in the old days, some services would only talk to clients
using privileged port numbers (<1024).

>> (LINK layer) probably has to be tied to the IP address; that is, a valid
>> "source" address needs to be supplied to .bind.

> Do you mean that an alias is not a valid source address? Because ping.c can 
> do it...

> I've also tried changing the port to  or 8080 but the same error happens.

On my laptop, 0 appears to be the only port number that bind accepts
for a raw socket.  Other numbers I tried all raise "socket.error:
[Errno 49] Can't assign requested address".

But this might depend on your OS.  What OS are you using?

Hope this helps,

-- HansM

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


Re: How do I display unicode value stored in a string variable using ord()

2012-08-22 Thread Hans Mulder
On 19/08/12 19:48:06, Paul Rubin wrote:
> Terry Reedy  writes:
>>  py> s = chr(0x + 1)
>>  py> a, b = s
> That looks like a 3.2- narrow build. Such which treat unicode strings
> as sequences of code units rather than sequences of codepoints. Not an
> implementation bug, but compromise design that goes back about a
> decade to when unicode was added to Python.

Actually, this compromise design was new in 3.0.

In 2.x, unicode strings were sequences of code points.
Narrow builds rejected any code points > 0x:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = unichr(0x + 1)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: unichr() arg not in range(0x1) (narrow Python build)


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


Re: Built-in open() with buffering > 1

2012-08-26 Thread Hans Mulder
On 24/08/12 06:35:27, Marco wrote:
> Please, can anyone explain me the meaning of the
> "buffering > 1" in the built-in open()?
> The doc says: "...and an integer > 1 to indicate the size
> of a fixed-size chunk buffer."
> So I thought this size was the number of bytes or chars, but
> it is not

The algorithm is explained at
http://docs.python.org/library/io.html#io.DEFAULT_BUFFER_SIZE

>> io.DEFAULT_BUFFER_SIZE
>>
>> An int containing the default buffer size used by the
>> module’s buffered I/O classes. open() uses the file’s
>> blksize (as obtained by os.stat()) if possible.

In other words: open() tries to find a suitable size by
calling os.stat(your_file).st_blksize and if that fails,
it uses io.DEFAULT_BUFFER_SIZE, which is 8192 on my box.

Whether you call open with buffering=2 or any larger
number, does not matter: the buffer size will be the
outcome of this algorithm.


Hope this helps,

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


Re: help with simple print statement!

2012-08-26 Thread Hans Mulder
On 24/08/12 21:59:12, Prasad, Ramit wrote:
> Also, print doesn't work inside a class.

It works for me:


> python3
Python 3.3.0a1 (v3.3.0a1:f1a9a6505731, Mar  4 2012, 12:26:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class spam(object):
... print("Hello there!")
...
Hello there!
>>>

I'm not sure why you'd want to call the print function in a
class definition, though.  The print function is more usually
called from within a method definition, not at the class level.


What, exactly are you doing?

Are you trying python2 syntax in a python3 interpreter, or vice versa?

Can you copy a complete session and paste it into a follow-up message?


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


Re: Computing win/loss records in Python

2012-08-26 Thread Hans Mulder
On 26/08/12 04:42:59, Steven W. Orr wrote:
> On 8/25/2012 10:20 PM, Christopher McComas wrote:
>> Greetings,
>>
>> I have code that I run via Django that grabs the results from various
>> sports from formatted text files. The script iterates over every line
>> in the formatted text files, finds the team in the Postgres database
>> updates their w/l record depending on the outcome on that line, saves
>> the team's row in the db, and then moves on to the next line in the file.
>>
>> I'm trying to get away from Django for this project, I want to run the
>> files, get the W/L results and output a formatted text file with the
>> teams and their W/L records. What's confusing me I guess how to store
>> the data/results as the wins and losses tally up. We're talking
>> hundreds of teams, thousands of games, but a quick example would be:
>>
>> Marshall
>> Ohio State
>> Kentucky
>> Indiana
>>
>> Marshall,24,Ohio State,48,
>> Kentucky,14,Indiana,10,
>> Marshall,10,Indiana,7,
>> Ohio State,28,Kentucky,10
>>
>> That's just a quick example, I can handle seperating the data in the
>> lines, figuring it all out, I just am unsure of how to keep a running
>> total of a team's record. I would do "for line in file:" then on the
>> first line I see that Marshall lost so they would have 1, Ohio State
>> won so they'd have 1 win. It'd go to the next line Kentucky 1 win,
>> Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd
>> have 1 win, but it would have to remember that loss from line 1...
>>
>> Does this make sense?
>>
>> Thanks,
> 
> win_count = defaultdict(int)
> loss_count = defaultdict(int)
> 
> items = line.split(',')
> if items[1] > items[3]:
> windex = 0
> lossdex = 2
> else:
> windex = 2
> lossdex = 0
> win_count[windex] += 1
> loss_count[lossdex] += 1

I think you meant:

win_count[items[windex]] += 1
loss_count[items[windex]] += 1


I think it would be more readable to do:

from collections import defaultdict

win_count = defaultdict(int)
loss_count = defaultdict(int)

items = line.split(',')
if int(items[1]) > int(items[3]):
winner = items[0]
loser = items[2]
else:
winner = items[2]
loser = items[0]
win_count[winner] += 1
loss_count[loser] += 1

It looks like you're afraid of wasting RAM by needlessly
copying strings.  However, this fear is unfounded: Python
doesn't copy strings, unless you tell it to do so explictly.
An assignment like "winner = items[0]" doesn't copy the string;
it merely creates a new reference to the existing string.


Hope this helps,

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


Re: sys.path in python3.3

2012-08-26 Thread Hans Mulder
On 26/08/12 21:21:15, Ned Deily wrote:
> In article 
> ,
>  Nicholas Cole  wrote:
>> In all previous versions of python, I've been able to install packages
>> into the path:
>>
>> ~/Library/Python/$py_version_short/site-packages
>>
>> but in the rc builds of python 3.3 this is no longer part of sys.path.
>>
>> Before I go hacking the install, is there a reason that this path was
>> removed?  Is there a recommended way to get it back, or is this a
>> gentle way of pushing us all to use virtualenv rather than installing
>> user-specific packages?
> 
> It should be working if you are using an OS X framework build.  What is 
> the value of sys.path?

Python 3.3.0rc1 (v3.3.0rc1:8bb5c7bc46ba, Aug 25 2012, 09:42:40)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for d in sys.path:
... print(d)
...

/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages


This is using the Python 3.3 for MacOS binary from python.org.


-- HansM



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


Re: sys.path in python3.3

2012-08-27 Thread Hans Mulder
On 26/08/12 20:47:34, Nicholas Cole wrote:
> Dear List,
> 
> In all previous versions of python, I've been able to install packages
> into the path:
> 
> ~/Library/Python/$py_version_short/site-packages
> 
> but in the rc builds of python 3.3 this is no longer part of sys.path.

It has been changed to

~/Library/Python/$py_version_short/lib/python/site-packages

You can find the path it's looking for in site.USER_SITE

> Before I go hacking the install, is there a reason that this path was
> removed?  Is there a recommended way to get it back, or is this a
> gentle way of pushing us all to use virtualenv rather than installing
> user-specific packages?

I don't know why it was changed.

It would be nice if there were some magic code you could use
for "install_lib" in your .pydistutils.cfg file that worked
in both 3.2 and 3.3.

-- HansM

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


Re: class object's attribute is also the instance's attribute?

2012-08-30 Thread Hans Mulder
On 30/08/12 14:34:51, Marco Nawijn wrote:

> Note that if you change 'd' it will change for all instances!

That depends on how you change it.

 bobj = A()
 bobj.d
> 'my attribute'
> 
 A.d = 'oops...attribute changed'

Here you change the attribute on the class.
That will affect all instances:

 aobj.d
> 'oops...attribute changed'
> 
 bobj.d
> 'oops...attribute changed'

You can also set the attribute on an instance:

>>> bobj.d = 'For bobj only'
>>> bobj.d
'For bobj only'
 aobj.d
> 'oops...attribute changed'

So, if you specifically change it on one instance, thenit won't
change on other instances of the same class.

> If you want attributes to be local to the instance, you have
> to define them in the __init__ section of the class like this:

That's a good idea, but it's not required.  You can set them
later, as shown above.


> class A(object):
> 
>def __init__(self):
> d = 'my attribute'

That will just set the global variable d.
You want to set the instance attribute:

self.d = 'my attribute'

 aobj = A()
 bobj = A()
> 
 aobj.d
> 'my attribute'

Note that aobj.d will not find the global variable d,
if neither the instance, nor the class nor any of the
base classes have that attribute.

I don't know where this 'my attribute' comes from, but
it's not the instance attribute you tried to set in the
__init__ method.  Maybe your class A still has a class
attribute with that value from an earlier experiment.


Hope this helps,

-- HansM

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


Re: class object's attribute is also the instance's attribute?

2012-08-30 Thread Hans Mulder
On 30/08/12 16:48:24, Marco Nawijn wrote:
> On Thursday, August 30, 2012 4:30:59 PM UTC+2, Dave Angel wrote:
>> On 08/30/2012 10:11 AM, Marco Nawijn wrote:
>>> On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
>>>> 

>>> Learned my lesson today. Don't assume you know something. Test it first ;).

A very important lesson.

Next week's lesson will be: if you test it first, then
paste it into a message for this forum, then tweak just
one unimportant detail, you'll need to test it again.

>>> I have done quite some programming in Python, but did not know that
class
>>> attributes are still local to the instances.

>> They're not.  They're just visible to the instances, except where the
>> instance has an instance attribute of the same name.  Don't be confused
>> by dir(), which shows both instance and class attributes.
>>
>> Please show me an example where you think you observe each instance
>> getting a copy of the class attribute.  There's probably some other
>> explanation.
> 
> I don't have an example. It was just what I thought would happen.
> Consider the following. In a class declaration like this:
> 
> class A(object):
> attr_1 = 10
> 
> def __init__(self):
>self.attr_2 = 20
> 
> If I instantiated it twice:
> 
> obj_1 = A()
> obj_2 = A()
> 
> For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after 
> the following statement:
> 
> obj_1.attr_1 = 12
> 
> is that obj_2.attr_1 also equals 12. This is what surprised me a little, 
> that's all. 

The trick is to look at obj_1.__dict__ to see what is defined locally:

>>> obj_1 = A()
>>> obj_1.__dict__
{'attr_2': 20}

>>> obj_1.attr_1 = 12
>>> obj_1.__dict__
{'attr_2': 20, 'attr_1': 12}


Hope this helps,

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


Re: Beginners question

2012-08-30 Thread Hans Mulder
On 30/08/12 14:49:54, Ulrich Eckhardt wrote:
> Am 30.08.2012 13:54, schrieb boltar2003@boltar.world:
> s = os.stat(".")
> print s
>> posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L,
>> st_nlink=2, st_u
>> id=1000, st_gid=100, st_size=4096L, st_atime=1346327745,
>> st_mtime=1346327754, st
>> _ctime=1346327754)
>>
>> What sort of object is posix.stat_result?
> 
> Use the type() function to find out. I guess that this is a named tuple,
> which is a tuple where the attributes are not indexed but have a name,
> see the documentation for the namedtuple() function from the collections
> library.

Named tuples were invented to do this kind of thing.

However, stat_result is fairly old, and named tuples
had not been invented back then.

If named tuples had been invented first, then os.stat
would probably have used them.

Hope this helps,

-- HansM


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


Re: is implemented with id ?

2012-09-05 Thread Hans Mulder
On 5/09/12 15:19:47, Franck Ditter wrote:
> Thanks to all, but :
> - 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.

Keep in mind that physical addresses can be reused when an
object is destroyed.  For example, in my Python3,


id(math.sqrt(17)) == id(math.cos(17))

returns True, even though the floats involved are different,
because the flaots have non-overlapping lifetimes and the
physical address happens to be reused.


Hope this helps,

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


Re: is implemented with id ?

2012-09-05 Thread Hans Mulder
On 5/09/12 17:09:30, Dave Angel wrote:
> But by claiming that id() really means address, and that those addresses
> might move during the lifetime of an object, then the fact that the id()
> functions are not called simultaneously implies that one object might
> move to where the other one used to be before the "move."

Whoa!  Not so fast!  The id() of an object is guaranteed to not
change during the object's lifetime.  So if an implementation
moves objects around (e.g. Jython), then it cannot use memory
addresses for the id() function.

> I think it much more likely that jython uses integer values for
> the id() function, and not physical addresses.

The id() function is guaranteed to return some flavour of integer.

In Jython, the return values are 1, 2, 3, 4, etc., except, of course,
if you invoke id() on an object you've id'd before, you get the same
number as before.

In current versions of CPython, you do get the (virtual) memory
address, converted to an int (or a long).  But then, CPython does
not move objects.

Maybe the next version of CPython should shift id values two or three
bits to the right, just to make sure people don't misinterpret ids as
memory addresses.


Hope this helps,

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


Re: How to print something only if it exists?

2012-09-07 Thread Hans Mulder
On 6/09/12 19:59:05, tinn...@isbd.co.uk wrote:
> I want to print a series of list elements some of which may not exist,
> e.g. I have a line:-
> 
>  print day, fld[1], balance, fld[2]
> 
> fld[2] doesn't always exist (fld is the result of a split) so the
> print fails when it isn't set.

How about:

 print day, fld[1], balance, fld[2] if len(fld) > 2 else ''


If you really want to avoid the keyword 'if', then you'd have to
do something like:

 print day, fld[1], balance, (fld[2:3] or [''])[0]

That may be shorter, but it isn't very readable.


Hope this helps,

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


Re: Division help in python

2012-09-08 Thread Hans Mulder
On 8/09/12 09:03:12, garabik-news-2005...@kassiopeia.juls.savba.sk wrote:
> Chris Angelico  wrote:
>> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla  wrote:
>>> I am brand new to python. checking over basic stuff. I came across the
>>> problem while doing so. If any body aware of the problem, kindly respond me.
>>>
>> 5/10
>>> 0
>> - 5/10
>>> -1
>>>
>>> The second case also should yield a 'zero' but it is giving a -1
>>
> ...
> 
>> The reason for this is that / (or in Python 3, //) rounds toward
>> negative infinity, not toward zero. This allows the modulo operator
> 
> I think he means the non-obvious unary minus precedence.

That seems unlikely.  Unary minus has lower precedence in
Python than in most other programming languages, but its
precedence is higher than division, so this example doesn't
show the difference.

For example, in C unary opeators have the highest precedence.
Yet -5/10 returns 0, not because of precedence, but because C
rounds towards zero.


Hope this helps,

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


Re: set and dict iteration

2012-09-08 Thread Hans Mulder
On 8/09/12 22:06:08, Thomas Rachel wrote:
> Am 19.08.2012 00:14 schrieb MRAB:
> 
>>> Can someone who is more familiar with the cycle detector and cycle
>>> breaker, help prove or disprove the above?
>>>
>> In simple terms, when you create an immutable object it can contain
>> only references to pre-existing objects, but in order to create a cycle
>> you need to make an object refer to another which is created later, so
>> it's not possible to create a cycle out of immutable objects.
> 
> Yes, but if I add a list in-between, I can create a refcycle:
> 
> a = []
> b = (a,)
> a.append(b)
> 
> So b is a tuple consisting of one list which in turn contains b.
> 
> It is not a direct cycle, but an indirect one.

It's a cycle and it contains an immutable object, but it's not
a cycle consisting of immutable objects only.

As MRAB was arguing: it is not possbible to create a cycle
consisting of immutable objects only (unless you do unspeakable
things at the C level).

-- HansM

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


Re: Newbie: where's the new python gone?

2012-09-09 Thread Hans Mulder
On 9/09/12 16:28:55, BobAalsma wrote:
> I think I've installed Python 2.7.3 according to the instructions in the 
> README, and now want to use that version. 
> However, when typing "python" in Terminal, I get "Python 2.6.4 (r264:75821M, 
> Oct 27 2009, 19:48:32) ".

Was that a freshly opened Terminal window, or one that was open
before the install?

The installers from python.org by default modify
your .bashrc file to put
/Library/Frameworks/Python.framework/Versions/2.7/bin
at the front of your shell's search path.

However, that only affects windows opened after the install
(unless you use "source ~/.bashrc" to read the new setting
into a pre-existing window).

What happens if you type:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python ?

> So:
> (1) I can't seem to find where the new software has gone and 
> (2) can't seem to find how to point to this new versoin.
> I've searched Python.org and with Google but :(
> [I'm on Mac OS X 10.7.4]

I'm on MacOS 10.5.0, and my default Python is
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

If yours isn't, you could try looking in finder at the
disk you installed Python to, and see if it has a top-level
folder named "Library", containing "Frameworks", etc.

If you find a "Python.framework" under /System/Library
that's the one that ships with MacOS.

Hope this helps,

-- HansM


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


Re: Newbie: where's the new python gone?

2012-09-10 Thread Hans Mulder
On 10/09/12 15:04:24, William R. Wing (Bill Wing) wrote:
> On Sep 9, 2012, at 10:28 AM, BobAalsma  wrote:
> 
>> I think I've installed Python 2.7.3 according to the instructions in the 
>> README, and now want to use that version. 
>> However, when typing "python" in Terminal, I get "Python 2.6.4 (r264:75821M, 
>> Oct 27 2009, 19:48:32) ".
>> So:
>> (1) I can't seem to find where the new software has gone and 
>> (2) can't seem to find how to point to this new versoin.
>> I've searched Python.org and with Google but :(
>> [I'm on Mac OS X 10.7.4]
>>
>> Please help.
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
> 
> Bob, I'm coming into this late, but it doesn't appear that you've
< gotten a satisfactory answer yet.  Let's take it one step at a time.
> 
> First, if none of the hints you've received earlier have gotten you going.
> Maybe the thing is to resort to a bigger hammer.  In a terminal window:
> 
> $sudo find / -name Python -print
> 
> This will search the entire file system for all the files named Python

Trouble is, the file you're looking for is named "python" and this
command is case-sensitive.  So the command you need would be:

  sudo find / -name python -print


> and will ask for your admin password so it can search in directories
> owned by root.

The file you're looking for is in a directory that you can read
with more mundane permissions, so you might want to leave off
the "sudo" prefix.  If you do, you'll get some message about
permission problems.


>  (It may also generate quite a bit of output, so you might want
> to capture it in a file.)

For example:

find / -name python > /tmp/pythons.txt 2> /dev/null

The 2>/dev/null bit throws away warnings about permission problems
and the like.

Alternatively, you can cut down the output like so:

find / -name python -print | grep bin/python

That will only report pythons found in directories named "bin".
On my laptop, that cuts the output down to:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/opt/local/bin/python
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/python
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python
/usr/bin/python
/usr/local/bin/python

Those are all valid python interpreters, or wrappers for same.

> In any case, this will take several minutes and while it is running,
> you can be checking a couple of other things.  OS X doesn't use a
> .bashrc file by default (you can make it do so if you want, but
> that's extra work right now).  It uses .login and then .profile
> to set up your python path _if_ you've used the installer from python.org.

I doubt it.  What files are used, depends on which shell you use.
Bash uses .profile; the C shell uses .login and .cshrc.

I don't think there is a shell that can read both .login and .profile
since .login typically uses C shell syntax and .profile uses Bourne
shell syntax.

If you're not sure which shell you have, type

echo $SHELL

at the shell prompt.

> So, look to see if you have a .profile in your ~ directory.  If so,
> then you're using (or have used at some point in the past) an installer
> from python.org.
> It should have an entry that looks something like the following:
> 
> # Setting PATH for Python 2.7
> # The original version is saved in .profile.pysave
> PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
> export PATH
> 
> Note the distinction between this path and the one from Apple.
> The python that ships from Apple is in /System/Library/Frameworks…
> 
> Do NOT touch the one from Apple.  Apple uses it for some of its
> housekeeping operations and you want it to stay just as Apple
> installed it.

+1

> When you finally find the Python 2.7 in the output from the "find"
> command, you can edit your .login (if you don't have a .profile) or
> edit .profile if you do.

Hope this helps,

-- HansM

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


Re: subprocess call is not waiting.

2012-09-14 Thread Hans Mulder
On 13/09/12 19:24:46, woo...@gmail.com wrote:
> It possibly requires a "shell=True",

That's almost always a bad idea, and wouldn't affect waiting anyway.

> but without any code or any way to test, we can not say.

That's very true.

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


Re: Moving folders with content

2012-09-15 Thread Hans Mulder
On 15/09/12 10:00:16, Nobody wrote:
> On Sat, 15 Sep 2012 04:36:00 +, jyoung79 wrote:
> 
>> I am working in both OS X Snow Leopard and Lion (10.6.8 and 10.7.4).  
>> I'm simply wanting to move folders (with their content) from various 
>> servers to the hard drive and then back to different directories on the 
>> servers.
>>
>> I want to be careful not to remove any metadata or resource forks from 
>> the files in the directories.  I did a bit of researching on shutil, and 
>> looks like it is similar to using "cp -p" and copystat(), which I believe 
>> will keep the resource fork, etc.
> 
> I don't think so. The shutil documentation says:
> 
>   Warning
> 
>   Even the higher-level file copying functions (copy(), copy2()) can’t
>   copy all file metadata.
> 
>   On POSIX platforms, this means that file owner and group are lost as 
> well
>   as ACLs. On Mac OS, the resource fork and other metadata are not used.
>   This means that resources will be lost and file type and creator codes
>   will not be correct. On Windows, file owners, ACLs and alternate data
>   streams are not copied.
> 
> The macostools module has functions which can copy the resource fork, but
> they aren't available in 64-bit builds and have been removed in Python 3.0.

You may want to use the subprocess module to run 'ditto'.  If
the destination folder does not exist, then ditto will copy MacOS
specific aspects such as resource forks, ACLs and HFS meta-data.

If the destination already exists, then ditto will copy file
contents, but not modify mode, ownership or ACLs of existing
folders inside the destination folder.

See the manual page for details.


Hope this helps,

-- HansM


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


Re: using text file to get ip address from hostname

2012-09-15 Thread Hans Mulder
On 15/09/12 18:20:42, Dan Katorza wrote:
> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
>> hello ,
>>
>>
>>
>> i'm new to Python and i searched the web and could not find an answer for my 
>> issue.
>>
>>
>>
>> i need to get an ip address from list of hostnames which are in a textfile.
>>
>>
>>
>> this is what i have so far 
>>
>> --
>>
>> #!/usr/bin/env python
>>
>> #Get the IP Address
>>
>>
>>
>> import socket
>>
>> hostname = 'need it to read from a text file'
>>
>> addr = socket.gethostbyname(hostname)
>>
>> print 'The address of ', hostname, 'is', addr 
>>
>>
>>
>> ---
>>
>>
>>
>> any idea ? 
>>
>> sorry for my english
>>
>>
>>
>> thanks.
> 
> hello again friends,
> thanks for everyone help on this.
> i guess i figured it out in two ways.
> the second one i prefer the most.
> 
> i will appreciate if someone can give me some tips.
> thanks again 
> 
> so...
> -
> First 
> -
> #!/usr/bin/env python
> #Get the IP Address
> 
> 
> print("hello, please enter file name here >"),

Instead of printing this string, you can pass it as the
argument to raw_input:

for line in open(raw_input("hello, please enter file name here> ")):

Cosmetically, I'd prefer a space after the '>'.

> import socket

PEP 8 recommends putting all imports at the top of the file, like you
do in your second script.

> for line in open(raw_input()):
> hostname = line.strip()
> print("IP address for {0} is 
> {1}.".format(hostname,socket.gethostbyname(hostname)))

To my mind, this line does two things: it finds the IP address and
prints it.  I think it would be more readable to do these on separate
lines:

ip_address = socket.gethostbyname(hostname)
print("IP address for {0} is {1}.".format(hostname, ip_address)

This forces you to find a good name for the value returned
by gethostbyname, which I consider a Good Thing (tm).

PEP 8 recommends that lines should not be longer than 80
characters.  Your line is longer than that; splitting it
in two conceptual steps nicely solves that issue as well.

> 
> second
> 
> #!/usr/bin/env python
> #Get the IP Address
> 
> import os
> 
> print("Hello, please enter file name here >"),
> FILENAME = raw_input()

PEP 8 recommends all upper case for constants, for example
socket.IPPROTO_IPV6.  The filename here is not a hard-wired
constant, so it should be in lower case.

> if os.path.isfile(FILENAME):

Your first script allowed me to enter "/dev/tty" at the prompt,
and then type a bunch of hostnames and an end-of-file character.
This script reports "FIle is missing or not reasable", because
/dev/tty is not a regular file.  I think the message should be
"File is missing or not readable or not a regular file".

I'm always annoyed when I get an error message with several
"or"s in it.  I prefer programs that figure out which of the
three potential issues is the case, and mention only one cause.

> print("\nFile Exist!")
> print("\nGetting ip from host name")
> print("\n")
> import socket
> for line in open (FILENAME):
> hostname = line.strip()
> print("IP address for {0} is 
> {1}.".format(hostname,socket.gethostbyname(hostname)))
> else:
> print ("\nFinished the operation")
> else:
> print ("\nFIle is missing or is not reasable"),

You don't want a comma at the end of this line: it messes
up the next shell prompt.

Also, this line a rather far away from the test that triggers it.

How about:

filename = raw_input("Hello, please enter file name here> ")
if not os.path.isfile(filename):
if not os.exist(filename):
print("\nFile {} does not exist")
else:
print("\nFile {} is not a regular file")
sys.exit(1)

print("\nFile {} exists", filename)
# etc.

Or you could skip the whole 'os' thing and use a try/except
construct instead:

#!/usr/bin/env python
#Get the IP Address

import sys, socket

filename = raw_input("Hello, please enter file name here> ")
try:
infile = open(filename)
except EnvironmentError as e:
print(e)
sys.exit(1)

print("\nFile {} exists!".format(filename))
print("\nGetting IP addresses for hosts")
print("\n")
for line in infile:
hostname = line.strip()
try:
ip_address = socket.gethostbyname(hostname)
except EnvironmentError as e:
print("Couldn't find IP address for {}: {}".format(hostname, e))
continue
print("IP address for {0} is {1}.".format(hostname, ip_address))
else:
print ("\nFinished the operation")


Using try/except has the advantage that it will correctly
report issues you didn't think about (for example, if file
exists, but you don't have 

Re: Moving folders with content

2012-09-16 Thread Hans Mulder
On 16/09/12 10:02:09, jyoun...@kc.rr.com wrote:
> Thank you  "Nobody" and Hans!

You're welcome!

>> You may want to use the subprocess module to run 'ditto'.  If
>> the destination folder does not exist, then ditto will copy MacOS
>> specific aspects such as resource forks, ACLs and HFS meta-data.
> 
> This looks like a good direction to go.  Maybe something like:
> 
>>>> import os
>>>> import subprocess
>>>>
>>>> p1 = os.path.expanduser('~/Desktop/IN/Test/')
>>>> p2 = os.path.expanduser('~/Desktop/OUT/Test/')
>>>>
>>>> cmd = 'ditto -vV "' + p1 + '" "' + p2 + '"'
>>>>
>>>> v = subprocess.check_output(cmd, shell=True)

This looks iffy: it would break if there would be any double
quotes in p1 or p2.  You might think that os.path.expanduser
would never expand '~' to something containing a double quote,
but you'd be wrong:

>>> import os
>>> os.environ['HOME'] = 'gotcha!"; rm -rf '
>>> print(os.path.expanduser('~/Desktop/IN/Test/'))
gotcha!"; rm -rf /Desktop/IN/Test/

It's easy and safer to avoid using 'shell=True' option:

cmd = ['ditto', '-vV', p1, p2]
v = subprocess.check_output(cmd, shell=False)

In this case, the safer version also happens to be shorter and
more readable.  But you should get into the habit of using
shell=False whenever possible, because it is much easier to
get it right.


Hope this helps,

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


Re: splitting numpy array unevenly

2012-09-18 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


Re: 'indent'ing Python in windows bat

2012-09-19 Thread Hans Mulder
On 18/09/12 05:01:14, Ian Kelly wrote:
> On Mon, Sep 17, 2012 at 7:08 PM, David Smith  wrote:
>> How do I "indent" if I have something like:
>> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
>> sys.exit(3)
> 
> How about:
> 
> if sR == 'Cope':
> sys.exit(1)
> elif sR == 'Perform':
> sys.exit(2)
> else:
> sys.exit(3)
> 
> I don't really understand why you're trying to keep it all on one line.

He's using Windows.

If he were on Unix, there'd be no problem:

python -c 'import sys
if sR == "Cope":
sys.exit(1)
elif sR == "Perform":
sys.exit(2)
else:
sys.exit(3) '

Unfortunately, the Windows shell doesn't do multi-line strings,
so he has to cram it all on one line.


-- HansM


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


Re: subprocess call is not waiting.

2012-09-19 Thread Hans Mulder
On 19/09/12 12:26:30, andrea crotti wrote:
> 2012/9/18 Dennis Lee Bieber :
>>
>> Unless you have a really massive result set from that "ls", that
>> command probably ran so fast that it is blocked waiting for someone to
>> read the PIPE.
> 
> I tried also with "ls -lR /" and that definitively takes a while to run,
> when I do this:
> 
> proc = subprocess.Popen(['ls', '-lR', '/'], stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> 
> nothing is running, only when I actually do
> proc.communicate()
> 
> I see the process running in top..
> Is it still an observation problem?

Yes: using "top" is an observation problem.

"Top", as the name suggests, shows only the most active processes.

It's quite possible that your 'ls' process is not active, because
it's waiting for your Python process to read some data from the pipe.

Try using "ps" instead.  Look in thte man page for the correct
options (they differ between platforms).  The default options do
not show all processes, so they may not show the process you're
looking for.

> Anyway I also need to know when the process is over while waiting, so
> probably a thread is the only way..

This sounds confused.

You don't need threads.  When 'ls' finishes, you'll read end-of-file
on the proc.stdout pipe.  You should then call proc.wait() to reap
its exit status (if you don't, you'll leave a zombie process).
Since the process has already finished, the proc.wait() call will
not actually do any waiting.


Hope this helps,

-- HansM


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


Re: subprocess call is not waiting.

2012-09-19 Thread Hans Mulder
On 19/09/12 18:34:58, andrea crotti wrote:
> 2012/9/19 Hans Mulder :
>> Yes: using "top" is an observation problem.
>>
>> "Top", as the name suggests, shows only the most active processes.
> 
> Sure but "ls -lR /" is a very active process if you try to run it..

Not necessarily:

>> It's quite possible that your 'ls' process is not active because
>> it's waiting for your Python process to read some data from the pipe.

Hope this helps,

-- HansM

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


Re: 'indent'ing Python in windows bat

2012-09-19 Thread Hans Mulder
On 19/09/12 19:51:44, Albert Hopkins wrote:
> On Tue, 2012-09-18 at 22:12 -0600, Jason Friedman wrote:
>>> I'm converting windows bat files little by little to Python 3 as I find time
>>> and learn Python.
>>> The most efficient method for some lines is to call Python like:
>>> python -c "import sys; sys.exit(3)"
>>>
>>> How do I "indent" if I have something like:
>>> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
>>> sys.exit(3)
>>
>> Some months ago I posted what I think is a similar question in the
>> Unix world:  I wanted to call a small portion of Python from within a
>> Bash script.
>>
>> Someone on this list answered (for Bash):
>>
>> #!/bin/bash
>> command1
>> command2
>> python -c "if True:
>> import module
>> if condition:
>> do_this
>> else:
>> do_that
>> "
>> command4
>> # end code
> 
> A better way (in *nix) would be, e.g.:
> 
> #!/bin/sh 
>
> 
> read -p 'Enter a number ' count
> 
> python << EOF
> print 'Odd numbers between 0 and ${count}'
> for i in range(${count}):
> if i % 2:
> print i
> EOF
> 
> Horribly bad example, but you get the idea.

If you do it like that, you have to remember to not use certain
punctuation characters in your Python code, because they are
meaningful to the shell, even inside a 

Re: sum works in sequences (Python 3)

2012-09-19 Thread Hans Mulder
On 19/09/12 17:07:04, Alister wrote:
> On Wed, 19 Sep 2012 16:41:20 +0200, Franck Ditter wrote:
> 
>> Hello,
>> I wonder why sum does not work on the string sequence in Python 3 :
>>
> sum((8,5,9,3))
>> 25
> sum([5,8,3,9,2])
>> 27
> sum('rtarze')
>> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>
>> I naively thought that sum('abc') would expand to 'a'+'b'+'c'
>> And the error message is somewhat cryptic...
>>
>> franck
> 
> Summation is a mathematical function that works on numbers
> Concatenation is the process of appending 1 string to another

Actually, the 'sum' builtin function is quite capable of
concatenatig objects, for example lists:

>>> sum(([2,3], [5,8], [13,21]), [])
[2, 3, 5, 8, 13, 21]

But if you pass a string as a starting value, you get an error:

>>> sum([], '')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: sum() can't sum strings [use ''.join(seq) instead]

In fact, you can bamboozle 'sum' into concatenating string by
by tricking it with a non-string starting value:

>>> class not_a_string(object):
...   def __add__(self, other):
... return other
...
>>> sum("rtarze", not_a_string())
'rtarze'
>>> sum(["Monty ", "Python", "'s Fly", "ing Ci", "rcus"],
... not_a_string())
"Monty Python's Flying Circus"
>>>


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  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: 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  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
>  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: 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  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  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: 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  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: 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: 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  wrote:
>> On Thu, 27 Sep 2012 22:25:39 + (UTC), John Gordon 
>> 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  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 "", line 1, in 
> 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  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: 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


  1   2   3   4   5   6   >