Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Richard D. Moores
On Fri, Jul 15, 2011 at 21:38, Steven D'Aprano  wrote:
>
> Richard D. Moores wrote:
>
>> But that makes me wonder if there isn't a simpler way to do it with
>> Python -- to delete the contents of a file without deleting the file?
>
> Opening a file for writing will flush the contents.
>
> open(filename, 'w')
>
> will do it, taking advantage of Python's garbage collector to (eventually) 
> close the file. The more careful way is hardly any harder:
>
> open(filename, 'w').close()
>
> and this ensures that the file isn't left open any longer than necessary.

open("C:/test/test.txt", 'w').close()

Good to know. Thanks, Steven.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDLE/tk in 10.6

2011-07-15 Thread Steven D'Aprano

Luke Thomas Mergner wrote:
[...]

I'd like to try IDLE but there appears to be a known bug with 10.6's version of 
ActiveTCL.  I've installed a newer version 8.5 via their website, but this has not fixed 
the problem. The module tkinter is still unable to load.  Since both python and activeTCL 
are installed as "Frameworks" on my mac, I wonder if I need to tell python27 
that there is another Tkinter installed elsewhere.  Can anyone give me some advice?



This is not really the sort of question we can likely help you with 
here. This is for learned about Python as a programming language, not 
the ins and outs of getting it running on various operating systems.


You might get lucky and find another Mac user here who can advise you, 
but you will probably have more luck on the main Python list, 
python-l...@python.com (also available on usenet, comp.lang.python) or 
on a more specialist Mac forum.



Good luck!



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Filling orders FIFO

2011-07-15 Thread Steven D'Aprano

Charles John wrote:

Hi I am new to python and was wondering what the best way to create an
order(bid and offer) queue, then match a bid and offer so that if
bid==offer, creates a filled order FIFO in python cgi using mysql? Does
anybody have any ideas? It would be greatly appreciated.


The simplest way to use a queue is with a list:

queue = []

You push items onto the queue with queue.append(item) and pop them off 
with queue.pop(0).


However, popping items may be slow if the queue grows very large (tens 
of thousands of items). It might be better to use a deque (double ended 
queue) instead of a list:


from collections import deque
queue = deque()

To push items onto the right hand side of the queue, then pop them off 
the left hand side:


queue.append(item)
queue.popleft()


As for the rest of your question, I don't understand what you mean by an 
order(bid and offer) queue. Perhaps you could give an example of what 
you mean.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Steven D'Aprano

Richard D. Moores wrote:


But that makes me wonder if there isn't a simpler way to do it with
Python -- to delete the contents of a file without deleting the file?


Opening a file for writing will flush the contents.

open(filename, 'w')

will do it, taking advantage of Python's garbage collector to 
(eventually) close the file. The more careful way is hardly any harder:


open(filename, 'w').close()

and this ensures that the file isn't left open any longer than necessary.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Richard D. Moores
On Fri, Jul 15, 2011 at 17:16, Dave Angel  wrote:
> On 07/15/2011 07:39 PM, Richard D. Moores wrote:

>> with open("C:/test/test.txt", "a") as file_object:
>>      print("Hello, world!", file=file_object)
>>
>> Yes, that works for me with Windows Vista. However, if test.txt is
>> empty, it puts in a blank line as line 1; line 2 is "Hello, world!".
>>
>> Dick
>> _
>
> I expect that your extra newline was already in the "empty" file.  It cannot
> have anything to do with using the forward slash for the filename.

I see that you are correct. It seems that selecting all the text in a
text file (in Notepad), then hitting the delete key doesn't guarantee
that the file will be left truly blank. The way that consistently
works for me is to place the cursor in the upper left corner of the
file and hold down the delete key.

Running

with open("C:/test/test.txt", "w") as file_object:
  print(file=file_object)

Also works.

But that makes me wonder if there isn't a simpler way to do it with
Python -- to delete the contents of a file without deleting the file?

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Dave Angel

On 07/15/2011 07:39 PM, Richard D. Moores wrote:

On Fri, Jul 15, 2011 at 16:21, xDog Walker  wrote:


I believe on Windows, you can almost always use a forward slash in a path:
C:/somewhere/somewhereelse/

with open("C:/test/test.txt", "a") as file_object:
  print("Hello, world!", file=file_object)

Yes, that works for me with Windows Vista. However, if test.txt is
empty, it puts in a blank line as line 1; line 2 is "Hello, world!".

Dick
_
I expect that your extra newline was already in the "empty" file.  It 
cannot have anything to do with using the forward slash for the filename.


DaveA



--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Richard D. Moores
On Fri, Jul 15, 2011 at 16:21, xDog Walker  wrote:

> I believe on Windows, you can almost always use a forward slash in a path:
> C:/somewhere/somewhereelse/

with open("C:/test/test.txt", "a") as file_object:
 print("Hello, world!", file=file_object)

Yes, that works for me with Windows Vista. However, if test.txt is
empty, it puts in a blank line as line 1; line 2 is "Hello, world!".

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread xDog Walker
On Friday 2011 July 15 15:58, Richard D. Moores wrote:
> On Fri, Jul 15, 2011 at 14:47, Stefan Behnel  wrote:
> > Richard D. Moores, 15.07.2011 23:21:
> >> What do I do to test.txt to make it "an object with a write(string)
> >> method"?
> >
> > Oh, there are countless ways to do that, e.g.
> >
> >  class Writable(object):
> >      def __init__(self, something):
> >          print("Found a %s" % something))
> >      def write(self, s):
> >          print(s)
> >
> >  print("Hello, world!", file=Writable("C:\\test\\test.txt"))
> >
> > However, I'm fairly sure what you want is this:
> >
> >    with open("C:\\test\\test.txt", "w") as file_object:
> >        print("Hello, world!", file=file_object)
>
> Yes, went with
>
> with open("C:\\test\\test.txt", "a+") as file_object:
>   print("Hello, world!", file=file_object)
>
> > Look up "open()" (open a file) and the "with statement" (used here
> > basically as a safe way to make sure the file is closed after writing).
> >
> > Also note that "\t" refers to a TAB character in Python, you used this
> > twice in your file path string.

I believe on Windows, you can almost always use a forward slash in a path: 
C:/somewhere/somewhereelse/

-- 
I have seen the future and I am not in it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Richard D. Moores
On Fri, Jul 15, 2011 at 14:47, Stefan Behnel  wrote:
> Richard D. Moores, 15.07.2011 23:21:

>> What do I do to test.txt to make it "an object with a write(string)
>> method"?
>
> Oh, there are countless ways to do that, e.g.
>
>  class Writable(object):
>      def __init__(self, something):
>          print("Found a %s" % something))
>      def write(self, s):
>          print(s)
>
>  print("Hello, world!", file=Writable("C:\\test\\test.txt"))
>
> However, I'm fairly sure what you want is this:
>
>    with open("C:\\test\\test.txt", "w") as file_object:
>        print("Hello, world!", file=file_object)

Yes, went with

with open("C:\\test\\test.txt", "a+") as file_object:
  print("Hello, world!", file=file_object)

> Look up "open()" (open a file) and the "with statement" (used here basically
> as a safe way to make sure the file is closed after writing).
>
> Also note that "\t" refers to a TAB character in Python, you used this twice
> in your file path string.

Oops. I'd forgotten about that.

Thanks very much, Stefan and Donald.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Stefan Behnel

Richard D. Moores, 15.07.2011 23:21:

On Sun, Jul 10, 2011 at 05:05, Peter Otten wrote:


>>> help(print)

shows

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep:  string inserted between values, default a space.
end:  string appended after the last value, default a newline.


I didn't know that printing to a file with print() was possible, so I tried

>>> print("Hello, world!", file="C:\test\test.txt")
Traceback (most recent call last):
   File "", line 1, in
builtins.AttributeError: 'str' object has no attribute 'write'
>>>

And the docs at
  tell me
"The file argument must be an object with a write(string) method; if
it is not present or None, sys.stdout will be used."

What do I do to test.txt to make it "an object with a write(string) method"?


Oh, there are countless ways to do that, e.g.

  class Writable(object):
  def __init__(self, something):
  print("Found a %s" % something))
  def write(self, s):
  print(s)

  print("Hello, world!", file=Writable("C:\\test\\test.txt"))

However, I'm fairly sure what you want is this:

with open("C:\\test\\test.txt", "w") as file_object:
print("Hello, world!", file=file_object)

Look up "open()" (open a file) and the "with statement" (used here 
basically as a safe way to make sure the file is closed after writing).


Also note that "\t" refers to a TAB character in Python, you used this 
twice in your file path string.


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello World in Python without space

2011-07-15 Thread Richard D. Moores
On Sun, Jul 10, 2011 at 05:05, Peter Otten <__pete...@web.de> wrote:

> >>> help(print)
>
> shows
>
> print(...)
>    print(value, ..., sep=' ', end='\n', file=sys.stdout)
>
>    Prints the values to a stream, or to sys.stdout by default.
>    Optional keyword arguments:
>    file: a file-like object (stream); defaults to the current sys.stdout.
>    sep:  string inserted between values, default a space.
>    end:  string appended after the last value, default a newline.

I didn't know that printing to a file with print() was possible, so I tried

>>> print("Hello, world!", file="C:\test\test.txt")
Traceback (most recent call last):
  File "", line 1, in 
builtins.AttributeError: 'str' object has no attribute 'write'
>>>

And the docs at
 tell me
"The file argument must be an object with a write(string) method; if
it is not present or None, sys.stdout will be used."

What do I do to test.txt to make it "an object with a write(string) method"?

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Filling orders FIFO

2011-07-15 Thread Charles John
Hi I am new to python and was wondering what the best way to create an
order(bid and offer) queue, then match a bid and offer so that if
bid==offer, creates a filled order FIFO in python cgi using mysql? Does
anybody have any ideas? It would be greatly appreciated.



Best
chuck
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] IDLE/tk in 10.6

2011-07-15 Thread Luke Thomas Mergner
Hi,

I am not a professional programmer, but just trying to learn.

I'm running Mac 10.6 Snow Leopard.  I used MacPorts to install python26, 
python27, and python3.  My python interpreter loads 2.7.2 after I ran the 
python_select command, which is added via MacPorts I think.  

I'd like to try IDLE but there appears to be a known bug with 10.6's version of 
ActiveTCL.  I've installed a newer version 8.5 via their website, but this has 
not fixed the problem. The module tkinter is still unable to load.  Since both 
python and activeTCL are installed as "Frameworks" on my mac, I wonder if I 
need to tell python27 that there is another Tkinter installed elsewhere.  Can 
anyone give me some advice?

>>> import Tkinter
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

Luke Mergner
lmerg...@gmail.com___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor