simple problem with jpype on WindowsXP

2011-03-26 Thread curtin

Hi,
I've got some kind of install problem with jpype ... It should just
work, but throws a dialog exception, that doesn't give a stack trace
so not very helpful.

This should be a simple install issue, but I've searched and haven't
seen any thing

import jpype
p = jpype.getDefaultJVMPath()
jpype.startJVM(p, "-ea")

# I choke on the startJVM ... JVM path is correct, I have no issue
with Java 1.6 installed

jpype:  JPype-0.5.4.win32-py2.7.exe

windows Python 2.7
ActivePython 2.7.0.2 (ActiveState Software Inc.) based on
Python 2.7 (r27:82500, Aug 23 2010, 17:18:21) [MSC v.1500 32 bit
(Intel)] on win32


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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-30 Thread Aahz
In article ,
Tim Chase   wrote:
>On 08/18/10 21:47, Steven D'Aprano wrote:
>>
>> Frankly, I think the OP doesn't really know what he wants, other than
>> premature optimization. It's amazing how popular that is :)
>
>You see, the trick to prematurely optimizing is to have a good 
>algorithm for prematurely optimizing...the real question them 
>becomes "How can I optimize my premature-optimization algorithms 
>to O(1) instead of O(newsgroup)?"
>
>:-)
>
>-tkc
>
>PS: I'm not positive, but O(newsgroup) may asymptotically 
>approach O(log n) if the question is well formed, but O(2^n) if
>flaming, indentation/line-length preferences, the meaning of OOP,
>SQL-parameter escaping, McNugget combinations, or suggestions that
>Python is "just a scripting language" are involved...

+1 QOTW
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"...if I were on life-support, I'd rather have it run by a Gameboy than a
Windows box."  --Cliff Wells
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Problem but tough for me if i want it in linear time

2010-08-18 Thread Tim Chase

On 08/18/10 21:47, Steven D'Aprano wrote:

Frankly, I think the OP doesn't really know what he wants, other than
premature optimization. It's amazing how popular that is :)


You see, the trick to prematurely optimizing is to have a good 
algorithm for prematurely optimizing...the real question them 
becomes "How can I optimize my premature-optimization algorithms 
to O(1) instead of O(newsgroup)?"


:-)

-tkc


PS: I'm not positive, but O(newsgroup) may asymptotically 
approach O(log n) if the question is well formed, but O(2^n) if 
flaming, indentation/line-length preferences, the meaning of OOP, 
SQL-parameter escaping, McNugget combinations, or suggestions 
that Python is "just a scripting language" are involved...






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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-18 Thread Steven D'Aprano
On Wed, 18 Aug 2010 16:03:58 +0200, Frederic Rentsch wrote:

> On Mon, 2010-08-16 at 23:17 +, Steven D'Aprano wrote:
>> On Mon, 16 Aug 2010 20:40:52 +0200, Frederic Rentsch wrote:
>> 
>> > How about
>> > 
>>  [obj for obj in dataList if obj.number == 100]
>> > 
>> > That should create a list of all objects whose .number is 100. No
>> > need to cycle through a loop.
>> 
>> What do you think the list comprehension does, if not cycle through a
>> loop?
>> 
>> 
>> --
>> Steven
> 
> What I think is that list comprehensions cycle through a loop a lot
> faster than a coded loop (for n in ...:). 

I think measurement beats intuition:


[st...@wow-wow ~]$ python -m timeit '[str(i) for i in xrange(10)]'
10 loops, best of 3: 84 msec per loop

[st...@wow-wow ~]$ python -m timeit 'L=[]
> for i in xrange(10):
> L.append(str(i))
> '
10 loops, best of 3: 105 msec per loop


But wait... we're not comparing apples with apples. There's an extra name 
lookup in the for-loop that the list comp doesn't have. We can fix that:


[st...@wow-wow ~]$ python -m timeit 'L=[]; append = L.append
for i in xrange(10):
append(str(i))
'
10 loops, best of 3: 86.7 msec per loop


The difference between 84 and 86 msec is essentially measurement error. 
Hell, the difference between 84 and 104 msec is not terribly significant 
either.



> As at the time of my post only
> coded loops had been proposed and the OP was concerned about speed, I
> thought I'd propose a list comprehension.

Yes, but the OP was concerned with asymptotic speed (big-oh notation), 
and both a for-loop and a list-comp are both O(N).

Frankly, I think the OP doesn't really know what he wants, other than 
premature optimization. It's amazing how popular that is :)



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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-18 Thread Frederic Rentsch
On Mon, 2010-08-16 at 23:17 +, Steven D'Aprano wrote:
> On Mon, 16 Aug 2010 20:40:52 +0200, Frederic Rentsch wrote:
> 
> > How about
> > 
>  [obj for obj in dataList if obj.number == 100]
> > 
> > That should create a list of all objects whose .number is 100. No need
> > to cycle through a loop. 
> 
> What do you think the list comprehension does, if not cycle through a 
> loop?
> 
> 
> -- 
> Steven

What I think is that list comprehensions cycle through a loop a lot
faster than a coded loop (for n in ...:). As at the time of my post only
coded loops had been proposed and the OP was concerned about speed, I
thought I'd propose a list comprehension. I guess my explanation was
poorly phrased. Thanks for the reminder.

Frederic


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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-16 Thread Steven D'Aprano
On Mon, 16 Aug 2010 20:40:52 +0200, Frederic Rentsch wrote:

> How about
> 
 [obj for obj in dataList if obj.number == 100]
> 
> That should create a list of all objects whose .number is 100. No need
> to cycle through a loop. 

What do you think the list comprehension does, if not cycle through a 
loop?


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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-16 Thread Frederic Rentsch
On Sun, 2010-08-15 at 15:14 +0200, Peter Otten wrote:
> ChrisChia wrote:
> 
> > dataList = [a, b, c, ...]
> > where a, b, c are objects of a Class X.
> > In Class X, it contains self.name and self.number
> > 
> > If i wish to test whether a number (let's say 100) appears in one of
> > the object, and return that object,
> > is that only fast way of solving this problem without iterating
> > through every object to see the number value?
> > 
> > dataList.__contains__ can only check my object instance name...
> > anyone can solve this in linear complexity?
> 
> Well, iteration as in 
> 
> next(item for item in dataList if item.number == 100) 
> 
> is O(n) and list.__contains__() has no magic way to do better. If you need 
> O(1) lookup you can use a dict or collections.defaultdict that maps 
> item.number to a list (or set) of X instances:
> 
> lookup = {}
> for item in dataList:
> lookup.setdefault(item.number, []).append(item)
> 
> print lookup[100]
> 
> 
> Peter
> 

How about 

>>> [obj for obj in dataList if obj.number == 100]

That should create a list of all objects whose .number is 100. No need
to cycle through a loop. If .number doesn't repeat get your object at
index 0. The approach may seem inefficient for the purpose of extracting
a single item, but the list needs to be gone through in any case and the
list comprehension is surely the most efficient way to do it. 

Frederic


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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-15 Thread Steven D'Aprano
On Sun, 15 Aug 2010 05:47:04 -0700, ChrisChia wrote:

> dataList = [a, b, c, ...]
> where a, b, c are objects of a Class X. In Class X, it contains
> self.name and self.number
> 
> If i wish to test whether a number (let's say 100) appears in one of the
> object, and return that object,
> is that only fast way of solving this problem without iterating through
> every object to see the number value?
> 
> dataList.__contains__ can only check my object instance name... 
> anyone can solve this in linear complexity?

If all you want is *linear* time, then simply iterating over the items 
will do the job:

for item in dataList:
if item.number == 100:
do_something_with(item)
break


If you want *less* than linear time, then keep the list sorted and do a 
binary search, which gives you O(log N) time. Don't sort the list each 
time, because that's O(N*log N). Or use a dict, which gives you O(1) time.


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


Re: Simple Problem but tough for me if i want it in linear time

2010-08-15 Thread MRAB

ChrisChia wrote:

dataList = [a, b, c, ...]
where a, b, c are objects of a Class X.
In Class X, it contains self.name and self.number

If i wish to test whether a number (let's say 100) appears in one of
the object, and return that object,
is that only fast way of solving this problem without iterating
through every object to see the number value?

dataList.__contains__ can only check my object instance name...
anyone can solve this in linear complexity?


Put them into a dict where the key is the number and the value is a list
of the objects with that number.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Problem but tough for me if i want it in linear time

2010-08-15 Thread Peter Otten
ChrisChia wrote:

> dataList = [a, b, c, ...]
> where a, b, c are objects of a Class X.
> In Class X, it contains self.name and self.number
> 
> If i wish to test whether a number (let's say 100) appears in one of
> the object, and return that object,
> is that only fast way of solving this problem without iterating
> through every object to see the number value?
> 
> dataList.__contains__ can only check my object instance name...
> anyone can solve this in linear complexity?

Well, iteration as in 

next(item for item in dataList if item.number == 100) 

is O(n) and list.__contains__() has no magic way to do better. If you need 
O(1) lookup you can use a dict or collections.defaultdict that maps 
item.number to a list (or set) of X instances:

lookup = {}
for item in dataList:
lookup.setdefault(item.number, []).append(item)

print lookup[100]


Peter



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


Simple Problem but tough for me if i want it in linear time

2010-08-15 Thread ChrisChia
dataList = [a, b, c, ...]
where a, b, c are objects of a Class X.
In Class X, it contains self.name and self.number

If i wish to test whether a number (let's say 100) appears in one of
the object, and return that object,
is that only fast way of solving this problem without iterating
through every object to see the number value?

dataList.__contains__ can only check my object instance name...
anyone can solve this in linear complexity?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple problem with lists I am just forgetting

2008-07-31 Thread Paul McGuire
On Jul 31, 2:51 pm, Alexnb <[EMAIL PROTECTED]> wrote:
> Lets say we have this list:
>
> funlist = ['a', 'b', 'c']
>
> and lets say I do this:
>
> if funlist[4]:
>     print funlist[4]
>
> I will get the exception "list index out of range"
>
> How can I test if the list item is empty without getting that exception?

Try using a slice.  Slices are ignorant of list index validity.

funlist = list("abc")  # too lazy to type ['a', 'b', 'c']
if funlist[4:]:
print funlist[4]

Voila! No exception!

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


Re: simple problem with lists I am just forgetting

2008-07-31 Thread bearophileHUGS
Alexnb:
> How can I test if the list item is empty without getting that exception?

In Python such list cell isn't empty, it's absent. So you can use
len(somelist) to see how much long the list is before accessing its
items. Often you can iterate on the list with a for, so you don't need
to care of the len().

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


simple problem with lists I am just forgetting

2008-07-31 Thread Alexnb

Lets say we have this list:

funlist = ['a', 'b', 'c']

and lets say I do this:

if funlist[4]:
print funlist[4]

I will get the exception "list index out of range"

How can I test if the list item is empty without getting that exception?
-- 
View this message in context: 
http://www.nabble.com/simple-problem-with-lists-I-am-just-forgetting-tp18762181p18762181.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: dynamically created names / simple problem

2008-03-25 Thread John Nagle
Robert Bossy wrote:
> Jules Stevenson wrote:
>>
>> Hello all,
>>
>> I'm fairly green to python and programming, so please go gently. The 
>> following code
>>
>> for display in secondary:
>>
>> self.("so_active_"+display) = wx.CheckBox(self.so_panel, -1, 
>> "checkbox_2")
>>
>> Errors, because of the apparent nastyness at the beginning. What I’m 
>> trying to do is loop through a list and create uniquely named wx 
>> widgets based on the list values. Obviously the above doesn’t work, 
>> and is probably naughty – what’s a good approach for achieving this?
>>
> Hi,
> 
> What you're looking for is the builtin function setattr:
> http://docs.python.org/lib/built-in-funcs.html#l2h-66

 Actually, you don't need to use attributes for this at all.
You're better off with an ordinary dictionary.  Something like this:

class someclass(object) :
def __init__(self) :
self.widgets = {} # empty dictionary

def addwidget(self, widgetname, widgetfn)
self.widgets[widgetname] = widgetfn

def callwidgetbyname(self, widgetname, args)
self.widgets[widgetname](*args)

The only reason to use attributes is when you want to allow the
use of the notation

objectinstance.attributename

If you're only going to get the attributes with getattr, they
don't need to be attributes.

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


RE: dynamically created names / simple problem

2008-03-25 Thread Jules Stevenson
Brilliant. Thanks



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


Re: dynamically created names / simple problem

2008-03-25 Thread Robert Bossy
Robert Bossy wrote:
> Jules Stevenson wrote:
>   
>> Hello all,
>>
>> I'm fairly green to python and programming, so please go gently. The 
>> following code
>>
>> for display in secondary:
>>
>> self.("so_active_"+display) = wx.CheckBox(self.so_panel, -1, "checkbox_2")
>>
>> Errors, because of the apparent nastyness at the beginning. What I’m 
>> trying to do is loop through a list and create uniquely named wx 
>> widgets based on the list values. Obviously the above doesn’t work, 
>> and is probably naughty – what’s a good approach for achieving this?
>>
>> 
> Hi,
>
> What you're looking for is the builtin function setattr:
> http://docs.python.org/lib/built-in-funcs.html#l2h-66
>
> Your snippet would be written (not tested):
>
> for display in secondary:
>
> setattr(self, "so_active_"+display, wx.CheckBox(self.so_panel, -1, 
> "checkbox_2"))
Damn! The indentation didn't came out right, it should be:

for display in secondary:
setattr(self, "so_active_"+display, wx.CheckBox(self.so_panel, 
-1,"checkbox_2"))

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


Re: dynamically created names / simple problem

2008-03-25 Thread Robert Bossy
Jules Stevenson wrote:
>
> Hello all,
>
> I'm fairly green to python and programming, so please go gently. The 
> following code
>
> for display in secondary:
>
> self.("so_active_"+display) = wx.CheckBox(self.so_panel, -1, "checkbox_2")
>
> Errors, because of the apparent nastyness at the beginning. What I’m 
> trying to do is loop through a list and create uniquely named wx 
> widgets based on the list values. Obviously the above doesn’t work, 
> and is probably naughty – what’s a good approach for achieving this?
>
Hi,

What you're looking for is the builtin function setattr:
http://docs.python.org/lib/built-in-funcs.html#l2h-66

Your snippet would be written (not tested):

for display in secondary:

setattr(self, "so_active_"+display, wx.CheckBox(self.so_panel, -1, 
"checkbox_2"))




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


dynamically created names / simple problem

2008-03-25 Thread Jules Stevenson
Hello all,

 

I'm fairly green to python and programming, so please go gently. The
following code

 

for display in secondary:

  self.("so_active_"+display) = wx.CheckBox(self.so_panel, -1,
"checkbox_2")

 

Errors, because of the apparent nastyness at the beginning. What I'm trying
to do is loop through a list and create uniquely named wx widgets based on
the list values. Obviously the above doesn't work, and is probably naughty -
what's a good approach for achieving this?

 

Many thanks,

 

Jules

 

 

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

Re: Simple problem with GUI!!

2006-10-27 Thread Fredrik Lundh
"mohan" wrote:

> At the end I had to close down my entire python compiler. I am using
> Python compiler with following specs in Windows XP OS.
>
> Pythonwin - Python IDE and GUI Framework for Windows.
> PythonWin 2.2.1 (#34, Feb 25 2003, 11:29:09) [MSC 32 bit (Intel)] on
> win32.
> Portions Copyright 1994-2001 Mark Hammond
>
> Please let me know why is this happening and also if I am missing
> something in the way of programming.

the PythonWin IDE doesn't work properly when you're running programs that
does their own Windows event handling.  this is a problem with PythonWin, not
with Tkinter or your program.  to solve this, you can either

1) check if you can make PythonWin run the program in a separate process

2) explicitly run the program in its own process (using "python.exe" or 
"pythonw.exe"),
   from the command line.

3) switch to an IDE that runs Python code in a separate process (such as IDLE,
   Wing, and most other modern IDE:s).

 



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


Simple problem with GUI!!

2006-10-27 Thread mohan
Hello Guys,

I am a beginner with Python programming and would like to implement
some GUI functionalities along with my main work. So I was trying to
implement one basic program (available from books).
Here is the code,


import Tkinter

TopLevelWindowObj = Tkinter.Tk() # Creating a root window
QuitObj = Tkinter.Button(TopLevelWindowObj, text = "QUIT", command =
TopLevelWindowObj.quit) #  Creating a Button
QuitObj.pack() # Positioning the button
Tkinter.mainloop()
#

So, this is a code to display a window with a "QUIT" button and by
clickign the button the entire window is closed. When I execute this
program, the window does not close down but the window is completely
disabled and after a few seconds I get the following message in a
message window,

**
Message box name - Microsoft Visual C++ Runtime Library

Runtime Error!

program...\Python22\core\lib\site-packages\Pythonwin\Pythonwin.exe

This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information.


At the end I had to close down my entire python compiler. I am using
Python compiler with following specs in Windows XP OS.

Pythonwin - Python IDE and GUI Framework for Windows.
PythonWin 2.2.1 (#34, Feb 25 2003, 11:29:09) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond

Please let me know why is this happening and also if I am missing
something in the way of programming.

Thanks in advance.

Cheers,
Mohan.

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


Re: Format a number as currency! I can't find any help on this simple problem.

2006-10-17 Thread Peter Otten
jr wrote:

>> >>> import locale
>> >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
>> 'English_United States.1252'

> Just tried the first 2 commands on win XP, Python 2.5 under Idle.
> An Error is raised: "unsupported locale setting" (lib/locale.py in
> setlocale, line 476).
> Actually I get the error also under Python 2.4.3
> Any idea what I'm missing?

Let your machine decide

>>> locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'

or try something less specific:

>>> locale.setlocale(locale.LC_ALL, "en_US")
'en_US'

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


Re: Format a number as currency! I can't find any help on this simple problem.

2006-10-17 Thread Jerry
Replace the conv function call with locale.localeconv.

--
Jerry

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


Re: Format a number as currency! I can't find any help on this simple problem.

2006-10-17 Thread jr
Hi, Bernard.
Just tried the first 2 commands on win XP, Python 2.5 under Idle.
An Error is raised: "unsupported locale setting" (lib/locale.py in
setlocale, line 476).
Actually I get the error also under Python 2.4.3
Any idea what I'm missing?
Thanks in advance.
Jürgen
Bernard wrote:
> >>> import locale
> >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
> 'English_United States.1252'
> >>> conv = locale.localeconv()
> >>> x = 1234567.8
> >>> locale.format("%d", x, grouping=True)
> '1,234,567'
> >>> locale.format("%s%.*f", (conv['currency_symbol'], 
> >>> conv['int_frac_digits'], x), grouping=True)
> '$1,234,567.80'
>
> Hi Richards,
>
> This works for me. I agree it's a bit complicated compared to C# but it
> works. I'd put it in a function if I had to use it.
>
>
> Richard Kessler wrote:
> > I am relatively new to Python.  Love it, but I find things that I can do
> > easily in .NET and cannot find a way to do in Python. I need to format a
> > number as currency, for example 12343.56 to $12,343.56.
> >
> > In C# all I need to do is decimal x = 12343.56 then 
> > x.ToString("$###,###.00");
> >
> > I cannot find a way to do this in Python. I must be missing something very
> > simple here. I have tried the locale module but it will not put in the 
> > commas.
> > I hope I do not have to write my own formatting routine...surely one is out
> > there and I just can't find it.
> >
> > Running on XP Pro. Python 2.4.3.
> > 
> > Thanks much in advance,
> > 
> > Richard

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


Re: Format a number as currency! I can't find any help on this simple problem.

2006-10-10 Thread Ben Finney
Richard Kessler <[EMAIL PROTECTED]> writes:

> I am relatively new to Python.  Love it, but I find things that I
> can do easily in .NET and cannot find a way to do in Python. I need
> to format a number as currency, for example 12343.56 to $12,343.56.
>
> In C# all I need to do is decimal x = 12343.56 then
> x.ToString("$###,###.00");

Rather than implement a custom format, Python provides the 'locale'
module to handle locale-specific things like digit-grouping.

http://docs.python.org/lib/module-locale>

-- 
 \  "About four years ago, I was -- no, it was yesterday."  -- |
  `\ Steven Wright |
_o__)  |
Ben Finney

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


Re: Format a number as currency! I can't find any help on this simple problem.

2006-10-10 Thread Bernard
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format("%s%.*f", (conv['currency_symbol'], conv['int_frac_digits'], 
>>> x), grouping=True)
'$1,234,567.80'

Hi Richards,

This works for me. I agree it's a bit complicated compared to C# but it
works. I'd put it in a function if I had to use it.


Richard Kessler wrote:
> I am relatively new to Python.  Love it, but I find things that I can do
> easily in .NET and cannot find a way to do in Python. I need to format a
> number as currency, for example 12343.56 to $12,343.56.
>
> In C# all I need to do is decimal x = 12343.56 then x.ToString("$###,###.00");
>
> I cannot find a way to do this in Python. I must be missing something very
> simple here. I have tried the locale module but it will not put in the commas.
> I hope I do not have to write my own formatting routine...surely one is out
> there and I just can't find it.
>
> Running on XP Pro. Python 2.4.3.
> 
> Thanks much in advance,
> 
> Richard

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


Format a number as currency! I can't find any help on this simple problem.

2006-10-10 Thread Richard Kessler
I am relatively new to Python.  Love it, but I find things that I can do 
easily in .NET and cannot find a way to do in Python. I need to format a 
number as currency, for example 12343.56 to $12,343.56.

In C# all I need to do is decimal x = 12343.56 then x.ToString("$###,###.00");

I cannot find a way to do this in Python. I must be missing something very 
simple here. I have tried the locale module but it will not put in the commas. 
I hope I do not have to write my own formatting routine...surely one is out 
there and I just can't find it.

Running on XP Pro. Python 2.4.3.

Thanks much in advance,

Richard

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


Re: pysqlite - simple problem

2006-09-03 Thread rdrink
Dennis Lee Bieber wrote:

>   That is probably the worst way to "fix" the problem -- as in the
> future, you may end up trying that method for something that may need to
> be quoted or escaped.
>
>   cur.execute(template, (arg1,) )
>
> allows the DB-API spec to properly convert the argument to the string
> format (quoted or escaped) as needed.

Thank you Dennis, point taken.
I will upgrade to pysqlite2 as soon as possible.

>the "pyformat" parameter style means that you're supposed to use "%s"
>instead of "?" for the placeholders:
>
> cur.execute("INSERT INTO foo (id) VALUES (%s)", (num,))

Thanks Fredrick, that seems so obvious now!

> (I'm sure this is mentioned in the fine manual, btw ;-)

... I guess I have must have missed it ;-)

>while string formatting works, and is safe for simple cases like this,
>it can quickly turn into a performance and security problem.  better
>avoid it for anything other than command-line tinkering and throw-away
>scripts.

You are both right about the perils of a non-standard approach, which
could easily break. Fortunately in this case this is a private project,
so no worry there.
-
And while you are both being so helpful, May I ask anyother stupid
question?...
One of the columns of my table contains a rather large list of numbers
e.g. [12345, 76543, 89786, ... ] sometimes up to 500 entries long.
And when I defined my table I set this column to text.
But the problem with that approach is of course then that it gets
returned as a string (which just happens to look like a list!) and I
can't iter over it. However I can use rsplit(','), with the exception
of the leading and trailing '[' ']', and I could fix that too... but
that's not the point... the real question is: Is there a way to have
python interperate the string "[ a,b,c ]" as a list? (and yes I have be
reading up on typing)...
OR
Is there a better way to store this in sqlite, ala a BLOB or encoded?

Thanks
Robb

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


Re: pysqlite - simple problem

2006-09-01 Thread Fredrik Lundh
rdrink wrote:


> And yes I should prolly move to pysqlite2, but for now I was able to
> fix it this way...
> num = 200
> mess = "INSERT INTO foo (id) VALUES (%s)" % num
> cur.execute(mess)
> 
> ... don't know why I didn't think of that last (oh wait, Yes I do...
> because 'last night' was actually 2am this morning, after working all
> day!)

the "pyformat" parameter style means that you're supposed to use "%s" 
instead of "?" for the placeholders:

 cur.execute("INSERT INTO foo (id) VALUES (%s)", (num,))

while string formatting works, and is safe for simple cases like this, 
it can quickly turn into a performance and security problem.  better 
avoid it for anything other than command-line tinkering and throw-away 
scripts.

(I'm sure this is mentioned in the fine manual, btw ;-)



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


Re: pysqlite - simple problem

2006-09-01 Thread rdrink
Thanks everyone!
But... RTFM? Ouch. It's not like I don't know what I'm doing :-(

... rather, that I *am* using the older sqlite module
> print sqlite.paramstyle = pyformat
> print sqlite.version = 1.0.1
 which does not support the qmark sytax. (and I fell victim of
someone elses tutorial).

And yes I should prolly move to pysqlite2, but for now I was able to
fix it this way...
num = 200
mess = "INSERT INTO foo (id) VALUES (%s)" % num
cur.execute(mess)

... don't know why I didn't think of that last (oh wait, Yes I do...
because 'last night' was actually 2am this morning, after working all
day!)

But thanks again to all of you for your help.

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


Re: pysqlite - simple problem

2006-09-01 Thread John Machin

Fredrik Lundh wrote:
> John Machin wrote:
> >> So this has to be something stupidly simple... but for the life of me I
> >> can't see it.
> >
> > With the '?' paramstyle, the 2nd arg to cursor.execute() should be a
> > *sequence* (typically a tuple) of the values that you are inserting.
> >
> > Tty this:
> > cur.execute("INSERT INTO foo (id) VALUES (?)", (num, ))
> >
> > This is standard Python DBAPI stuff - you would probably get a similar
> > response from other gadgets e.g. mySQLdb -- IOW it's not specific to
> > pysqlite.
>
> that mistake gives an entirely different error message, at least under 2.2.0
> (which is the version shipped with 2.5):
>

You're right. I didn't spot that the OP may be using an antique:

File "/usr/lib/python2.4/site-packages/sqlite/main.py"

So the advice has to be augmented:
1. Update to latest pysqlite2 (which BTW is a later version that that
shipped with Python 2.5, just to add some confusion)
2. Pass values as a sequence

Cheers,
John

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


Re: pysqlite - simple problem

2006-09-01 Thread Fredrik Lundh
John Machin wrote:
>> So this has to be something stupidly simple... but for the life of me I
>> can't see it.
>
> With the '?' paramstyle, the 2nd arg to cursor.execute() should be a
> *sequence* (typically a tuple) of the values that you are inserting.
>
> Tty this:
> cur.execute("INSERT INTO foo (id) VALUES (?)", (num, ))
>
> This is standard Python DBAPI stuff - you would probably get a similar
> response from other gadgets e.g. mySQLdb -- IOW it's not specific to
> pysqlite.

that mistake gives an entirely different error message, at least under 2.2.0
(which is the version shipped with 2.5):

>>> import sqlite3
>>> db = sqlite3.connect("foo.db")
>>> cur = db.cursor()
>>> cur.execute("CREATE TABLE foo (id INTEGER)")

>>> cur.execute("INSERT INTO foo (id) VALUES (?)", 200)
Traceback (most recent call last):
  File "", line 1, in 
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current
statement uses 1, and there are -1 supplied.
>>> cur.execute("INSERT INTO foo (id) VALUES (?)", [200])


(not sure "-1 arguments supplied" is any less confusing, though)

 



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


Re: pysqlite - simple problem

2006-09-01 Thread John Machin
rdrink wrote:
> I am just getting into pysqlite (with a fair amount of Python and MySQL
> experience behind me) and have coded a simple test case to try to get
> the hang of things...
> yet have run into a 'stock simple' problem...
>
> I can create a database 'test.db', add a table 'foo' (which BTW I
> repeatedly DROP on each run) with one INTGER column 'id', and can
> insert data into with:
> cur.execute("INSERT INTO foo (id) VALUES (200)")
> con.commit()
> (and fetch back out)
> all with no problem. But...
>
> If I try to expand this to:
> num = 200
> cur.execute("INSERT INTO foo (id) VALUES (?)", num)
> I get the error...
> Traceback (most recent call last):
>   File "/home/rdrink/Programming/Python/Inner_Square/sqlite_test.py",
> line 46, in ?
> cur.execute("INSERT INTO foo (id) VALUES (?)", num)
>   File "/usr/lib/python2.4/site-packages/sqlite/main.py", line 255, in
> execute
> self.rs = self.con.db.execute(SQL % parms)
> TypeError: not all arguments converted during string formatting
> ... which obviously points to a 'typing' problem.
> ?? but where ??
> >From all the docs I have read Python 'int' and sqlite INTEGER should
> pass back and forth seemlessly...
> And I have even tried to reduce things to simply:
> cur.execute("INSERT INTO foo (id) VALUES (?)", 200)
> but still raise the same error.
>
> So this has to be something stupidly simple... but for the life of me I
> can't see it.

With the '?' paramstyle, the 2nd arg to cursor.execute() should be a
*sequence* (typically a tuple) of the values that you are inserting.

Tty this:
cur.execute("INSERT INTO foo (id) VALUES (?)", (num, ))

This is standard Python DBAPI stuff - you would probably get a similar
response from other gadgets e.g. mySQLdb -- IOW it's not specific to
pysqlite.

> Advice, suggestions, pointers for the noob?

General advice: Read the docs -- both the gadget-specific docs and the
Python DBAPI spec (found at http://www.python.org/dev/peps/pep-0249/).

HTH,
John

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


Re: pysqlite - simple problem

2006-09-01 Thread Fredrik Lundh
"rdrink" <[EMAIL PROTECTED]> wrote:

>I am just getting into pysqlite (with a fair amount of Python and MySQL
> experience behind me) and have coded a simple test case to try to get
> the hang of things...
>
> yet have run into a 'stock simple' problem...

what does

import sqlite
print sqlite.paramstyle
print sqlite.version

print on your machine ?

(afaik, version 1 of the python bindings use paramstyle=pyformat, version
2 uses qmark.  maybe you have a version 1 library ?)

 



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


Re: pysqlite - simple problem

2006-09-01 Thread Gerold Penz
rdrink schrieb:
> num = 200
> cur.execute("INSERT INTO foo (id) VALUES (?)", num)

Hi!

``num`` must be an iterable object (tuple, list, ...).

   num = (200,)
   cur.execute("INSERT INTO foo (id) VALUES (?)", num)

Regards,
Gerold
:-)

-- 

Gerold Penz - bcom - Programmierung
 [EMAIL PROTECTED] | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
 wirksamsten Erfolgsfaktoren. Dale Carnegie
-- 
http://mail.python.org/mailman/listinfo/python-list


pysqlite - simple problem

2006-09-01 Thread rdrink
I am just getting into pysqlite (with a fair amount of Python and MySQL
experience behind me) and have coded a simple test case to try to get
the hang of things...
yet have run into a 'stock simple' problem...

I can create a database 'test.db', add a table 'foo' (which BTW I
repeatedly DROP on each run) with one INTGER column 'id', and can
insert data into with:
cur.execute("INSERT INTO foo (id) VALUES (200)")
con.commit()
(and fetch back out)
all with no problem. But...

If I try to expand this to:
num = 200
cur.execute("INSERT INTO foo (id) VALUES (?)", num)
I get the error...
Traceback (most recent call last):
  File "/home/rdrink/Programming/Python/Inner_Square/sqlite_test.py",
line 46, in ?
cur.execute("INSERT INTO foo (id) VALUES (?)", num)
  File "/usr/lib/python2.4/site-packages/sqlite/main.py", line 255, in
execute
self.rs = self.con.db.execute(SQL % parms)
TypeError: not all arguments converted during string formatting
... which obviously points to a 'typing' problem.
?? but where ??
>From all the docs I have read Python 'int' and sqlite INTEGER should
pass back and forth seemlessly...
And I have even tried to reduce things to simply:
cur.execute("INSERT INTO foo (id) VALUES (?)", 200)
but still raise the same error.

So this has to be something stupidly simple... but for the life of me I
can't see it.

Advice, suggestions, pointers for the noob?

rd

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


Python, VB math simple problem

2006-03-31 Thread Mr X






Hi looking for help with what should be a fairly simple Python problem, relating to VB inter-operability.

Got a great response from a fellow named Matt at [EMAIL PROTECTED], pointed me in some good directions - some areas, concerns still foggy on, the below thread is included any feedback on this simple dilemma

would be very appreciated.



Thanks,



D



thread follows below;





 

To: 	[EMAIL PROTECTED]	   

Subject: 	Problem with Python math functions and VB	   

Date: 	3/30/2006 9:39:28 PM	   

 Download Message Display Headers Printer Friendly 			 

Previous | Next 



Wondering if you might either know how to solve the following.



I've a background in Visual Basic, and am using an old version, 4.0, it compiles to a smaller executable which I prefer. I find myself in an odd situation, needing a very simple yet powerful capability of Python for a VB app

Im working on.



Simply, a large 300 digit number is divided by a smaller number ranging from 1 to 3 digits. I.e;



This large 300 digit number is generated as a string from the VB app, and I want to input this somehow

from the VB app directly to Python for simple math operations.



Where; x = 300 digit number, y = divisor ( say '37')





x / 37



I want to divide x by y but I want the remainder of this division to at least 3 or 4 decimal places, so my Python script at the command line;



x %y /y. = z



So now I want to take the resultant, the full number plus its remainder, and I want to round this number up

to the next highest number and divide it by the same constant;



z rounded up to next highest number (never the lowest)



so



z /y = z Long



Remove the 'L' at the end, round up the last digit of z = Z



Then;



Z %y /y. = a



Then I want the last five digits of z (not Z) and a truncated at the end, so the last digit before

the decimal point and the four digits past the decimal point printed to a text file.



I want to be able to open the text file with the VB app and use this data as inputs.

==



Ok, so here is my dilemma, I know VERY litle about Python and a fair bit about VB.



Ideally, I'd love to be able to simply have some extremely small executable that just accepts inputs

does the calculations above and then spits out the outputs. If it were possible to write some

simple lines of math code in Python and then compile these scripts in Python to a Windows

compatible executable,that would be fantastic.



If I could simply have my VB app, 'call' the name of the tiny Python executable, and then the Python executable

just automatically looked for a named text file (created by the VB app) and extracted the 300 digit number from this, then performed the calcs, then spit this data out as a new text file name it created, which I could then use the VB app to open and read from, THAT would be ideal.



However, I don't know if Python can compile scripts to an exe? If it can how could I find out how to do this?



If it doesn't, how could I get VB to directly pass commands to the Python command line and then automatically

extract the outputs? Shelling out from VB to Python would be tough to the command line I think, since the Python command line uses the 'Edit / Mark, Paste' approach to inserting, copy inputs, outputs and this would be virtually untenable, as far as I can tell to automate in a VB shell out routine.



So basically, how the heck can I access Pythons ability to perform simple calculations on very large numbers, easily, from within VB 4.0 ? There must be a way, it seems like such a simple think to do, especially since the actual math operations are so simple, straight forward, and would never change.



Any ideas?







--

Matthew,  thanks for your response. 



<-Original Message-> 

>From: Matthew Dixon Cowles

>Sent: 3/31/2006 9:41:18 AM

>To: [EMAIL PROTECTED]

>Cc: [EMAIL PROTECTED]

>Subject: Re: [Python-Help] Problem with Python math functions and VB



>I'm sure that there's a way to do that, but I'm not familiar with 

>Visual Basic and I don't know what inter-process communication 

>facilities it offers. 



Is there a person or group you might direct me to that has worked with this 

inter-process communication between VB and Python?



>I don't think that Python is going to be able to do that for you out 

>of the box. Hundreds of digits of floating-point precision is a lot. 



Could you explain that a bit more, sorry Im not sure what you mean

by 'out of the box' ?  If I run the Python command line screen in windows

and manually type out a very large number, say 180 digits; where 'X' = very large number;



X %37 /37.



returns what Im after, value wise.  but of course I don't want to do this manually each time

for every dividend.



>You might find that one of the Python modules that let you use an 

>extended-precision library would do what you want. GMPY is one: 



>http://gmpy.sourceforge.net/ 


Python, VB math simple problem

2006-03-31 Thread Mr X






Hi looking for help with what should be a fairly simple Python problem, relating to VB inter-operability.

Got a great response from a fellow named Matt at [EMAIL PROTECTED], pointed me in some good directions - some areas, concerns still foggy on, the below thread is included any feedback on this simple dilemma

would be very appreciated.



Thanks,



D



thread follows below;





 

To: 	[EMAIL PROTECTED]	   

Subject: 	Problem with Python math functions and VB	   

Date: 	3/30/2006 9:39:28 PM	   

 Download Message Display Headers Printer Friendly 			 

Previous | Next 



Wondering if you might either know how to solve the following.



I've a background in Visual Basic, and am using an old version, 4.0, it compiles to a smaller executable which I prefer. I find myself in an odd situation, needing a very simple yet powerful capability of Python for a VB app

Im working on.



Simply, a large 300 digit number is divided by a smaller number ranging from 1 to 3 digits. I.e;



This large 300 digit number is generated as a string from the VB app, and I want to input this somehow

from the VB app directly to Python for simple math operations.



Where; x = 300 digit number, y = divisor ( say '37')





x / 37



I want to divide x by y but I want the remainder of this division to at least 3 or 4 decimal places, so my Python script at the command line;



x %y /y. = z



So now I want to take the resultant, the full number plus its remainder, and I want to round this number up

to the next highest number and divide it by the same constant;



z rounded up to next highest number (never the lowest)



so



z /y = z Long



Remove the 'L' at the end, round up the last digit of z = Z



Then;



Z %y /y. = a



Then I want the last five digits of z (not Z) and a truncated at the end, so the last digit before

the decimal point and the four digits past the decimal point printed to a text file.



I want to be able to open the text file with the VB app and use this data as inputs.

==



Ok, so here is my dilemma, I know VERY litle about Python and a fair bit about VB.



Ideally, I'd love to be able to simply have some extremely small executable that just accepts inputs

does the calculations above and then spits out the outputs. If it were possible to write some

simple lines of math code in Python and then compile these scripts in Python to a Windows

compatible executable,that would be fantastic.



If I could simply have my VB app, 'call' the name of the tiny Python executable, and then the Python executable

just automatically looked for a named text file (created by the VB app) and extracted the 300 digit number from this, then performed the calcs, then spit this data out as a new text file name it created, which I could then use the VB app to open and read from, THAT would be ideal.



However, I don't know if Python can compile scripts to an exe? If it can how could I find out how to do this?



If it doesn't, how could I get VB to directly pass commands to the Python command line and then automatically

extract the outputs? Shelling out from VB to Python would be tough to the command line I think, since the Python command line uses the 'Edit / Mark, Paste' approach to inserting, copy inputs, outputs and this would be virtually untenable, as far as I can tell to automate in a VB shell out routine.



So basically, how the heck can I access Pythons ability to perform simple calculations on very large numbers, easily, from within VB 4.0 ? There must be a way, it seems like such a simple think to do, especially since the actual math operations are so simple, straight forward, and would never change.



Any ideas?







--

Matthew,  thanks for your response. 



<-Original Message-> 

>From: Matthew Dixon Cowles

>Sent: 3/31/2006 9:41:18 AM

>To: [EMAIL PROTECTED]

>Cc: [EMAIL PROTECTED]

>Subject: Re: [Python-Help] Problem with Python math functions and VB



>I'm sure that there's a way to do that, but I'm not familiar with 

>Visual Basic and I don't know what inter-process communication 

>facilities it offers. 



Is there a person or group you might direct me to that has worked with this 

inter-process communication between VB and Python?



>I don't think that Python is going to be able to do that for you out 

>of the box. Hundreds of digits of floating-point precision is a lot. 



Could you explain that a bit more, sorry Im not sure what you mean

by 'out of the box' ?  If I run the Python command line screen in windows

and manually type out a very large number, say 180 digits; where 'X' = very large number;



X %37 /37.



returns what Im after, value wise.  but of course I don't want to do this manually each time

for every dividend.



>You might find that one of the Python modules that let you use an 

>extended-precision library would do what you want. GMPY is one: 



>http://gmpy.sourceforge.net/ 


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Peter Hansen
Tom wrote:
> Peter Hansen wrote:
>> Where do you think those double quotation marks came from?  What 
>> happens if you try the following instead of using the variables you 
>> were trying to use?
>>
>> os.rename("e:\\music\\Joni Mitchell\\ogg-8",
>>  "e:\\music.ogg\\Joni Mitchell\\ogg-8")
>>
>> Now try it with this and observe how you get (I predict) the same 
>> error message as you originally got, and note what your mistake was:
>>
>> os.rename('"e:\\music\\Joni Mitchell\\ogg-8"',
>>  '"e:\\music.ogg\\Joni Mitchell\\ogg-8"')
> 
> 
> This produced the msg:
> OSError: [Errno 22] Invalid argument

Presumably "this" means the second one, whereas for the first you got a 
different message?  The latter is clearly invalid, since paths can't 
contain quotation marks.  The former would work provided the folder 
"music.ogg/Joni Mitchell" existed.

> The problem seems to be that I'm trying to create more than one 
> directory at a time.  In the above example, the dir 'Joni Mitchell' 
> doesn't exist.

If that were true, and the only problem, you would get a different 
error: OSError: [Errno 2] No such file or directory

> The functions that I'm calling (os.rename and shutil.move) use mkdir, 
> not makedirs.  The solution is for me to use makedirs with all of the 
> path except the leaf before I move/rename the old dir.

Regardless of the issue with error messages, that sounds like it does 
explain your problem.  Great! :-)

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Peter Hansen wrote:
> Tom wrote:
> 
>>Drive E: is removable, so I was careful to verify that that was a factor 
>>in the problem.
>>
>>Yes, I can do the same renaming, with the same drive, at the command line.
>>
>>I think I put the emphasis in the wrong place in my question.  This 
>>isn't really about os.rename().  It is about putting a filename with 
>>spaces into a string object and then using it as a parameter to an 'os' 
>>command.
> 
> 
> That can't really be all there is to the issue, since other people can 
> successfully use spaces in filenames passed to 'os' commands including 
> os.rename.  There must be something special with _your_ situation.  What 
> else could it be except the file system?
> 
> Oh... wait, I see now.  Look closely at your error message:
> 
> Traceback (most recent call last):
>File "E:\Music\MoveMusic.py", line 64, in ?
>  main();
> ...
>File "E:\Music\MoveMusic.py", line 49, in Visit
>  os.mkdir( NewDir );
> OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni 
> Mitchell\\ogg-8"'
> 
> Where do you think those double quotation marks came from?  What happens 
> if you try the following instead of using the variables you were trying 
> to use?
> 
> os.rename("e:\\music\\Joni Mitchell\\ogg-8",
>  "e:\\music.ogg\\Joni Mitchell\\ogg-8")
> 
> Now try it with this and observe how you get (I predict) the same error 
> message as you originally got, and note what your mistake was:
> 
> os.rename('"e:\\music\\Joni Mitchell\\ogg-8"',
>  '"e:\\music.ogg\\Joni Mitchell\\ogg-8"')

This produced the msg:
OSError: [Errno 22] Invalid argument

I'm now using forward slashes instead of backslashes - this simplifies 
things a bit.

The problem seems to be that I'm trying to create more than one 
directory at a time.  In the above example, the dir 'Joni Mitchell' 
doesn't exist.

The functions that I'm calling (os.rename and shutil.move) use mkdir, 
not makedirs.  The solution is for me to use makedirs with all of the 
path except the leaf before I move/rename the old dir.

Thanks for your help,
Tom.


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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Peter Hansen
Tom wrote:
> Drive E: is removable, so I was careful to verify that that was a factor 
> in the problem.
> 
> Yes, I can do the same renaming, with the same drive, at the command line.
> 
> I think I put the emphasis in the wrong place in my question.  This 
> isn't really about os.rename().  It is about putting a filename with 
> spaces into a string object and then using it as a parameter to an 'os' 
> command.

That can't really be all there is to the issue, since other people can 
successfully use spaces in filenames passed to 'os' commands including 
os.rename.  There must be something special with _your_ situation.  What 
else could it be except the file system?

Oh... wait, I see now.  Look closely at your error message:

Traceback (most recent call last):
   File "E:\Music\MoveMusic.py", line 64, in ?
 main();
...
   File "E:\Music\MoveMusic.py", line 49, in Visit
 os.mkdir( NewDir );
OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni 
Mitchell\\ogg-8"'

Where do you think those double quotation marks came from?  What happens 
if you try the following instead of using the variables you were trying 
to use?

os.rename("e:\\music\\Joni Mitchell\\ogg-8",
 "e:\\music.ogg\\Joni Mitchell\\ogg-8")

Now try it with this and observe how you get (I predict) the same error 
message as you originally got, and note what your mistake was:

os.rename('"e:\\music\\Joni Mitchell\\ogg-8"',
 '"e:\\music.ogg\\Joni Mitchell\\ogg-8"')

(To avoid confusion, cut and paste the above lines rather than 
attempting to retype them.)

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Yes, I am sure about those things.
I've tried shutil.move and got the same result.
Forward slash?  I'll give that a try and report back here if it works.

Thanks,
Tom.

Larry Bates wrote:
> Are you sure the source directory exists and you
> have rights to rename it?  Because the rename works
> for me.
> 
> But you may want to look at shutil.move and/or
> use forward slashes (they work under Windows)
> 
> -Larry Bates
> 
> 
> Tom wrote:
> 
>>I'm having a problem using a path with spaces as a parameter to
>>os.rename() in a program on WinXP.
>>
>>This works fine at the command line (where the folder "c:\aa bb" exists)
>>
>>
>>>os.rename( "c\aa bb", "c:\cc dd" );
>>>
>>
>>But, I can't get it to work in my program, eg.
>>
>>print SrcDir
>>print NewDir
>>os.rename( SrcDir, NewDir );
>>
>>when I run this I get something like this:
>>
>>"e:\\music\\Joni Mitchell\\ogg-8"
>>"e:\\music.ogg\\Joni Mitchell\\ogg-8"
>>
>>Traceback (most recent call last):
>>  File "E:\Music\MoveMusic.py", line 64, in ?
>>main();
>>...
>>  File "E:\Music\MoveMusic.py", line 49, in Visit
>>os.mkdir( NewDir );
>>OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni
>>Mitchell\\ogg-8"'
>>
>>I've tried different combinations of single backslash vs. double
>>backslash, and quoted vs. non-quoted, but it always fails.
>>
>>The problem is not specific to os.rename.  If I instead use mkdir(
>>SrcDir ) I get the same problem.
>>
>>Thanks,
>>Tom.
>>

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Peter Hansen wrote:
> Tom wrote:
> 
>>I'm having a problem using a path with spaces as a parameter to 
>>os.rename() in a program on WinXP.
>>
>>This works fine at the command line (where the folder "c:\aa bb" exists)
>>
>> > os.rename( "c\aa bb", "c:\cc dd" );
>> >
>>
>>But, I can't get it to work in my program, eg.
>>
>>print SrcDir
>>print NewDir
>>os.rename( SrcDir, NewDir );
>>
>>when I run this I get something like this:
>>
>>"e:\\music\\Joni Mitchell\\ogg-8"
>>"e:\\music.ogg\\Joni Mitchell\\ogg-8"
> 
> 
> What kind of device is drive E: ?  Are you certain it allows spaces in 
> filenames?  Can you do the same renaming *using the same drive* at the 
> command line or from Explorer?
> 
> -Peter

Drive E: is removable, so I was careful to verify that that was a factor 
in the problem.

Yes, I can do the same renaming, with the same drive, at the command line.

I think I put the emphasis in the wrong place in my question.  This 
isn't really about os.rename().  It is about putting a filename with 
spaces into a string object and then using it as a parameter to an 'os' 
command.

Tom.

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-09 Thread Peter Hansen
Tom wrote:
> I'm having a problem using a path with spaces as a parameter to 
> os.rename() in a program on WinXP.
> 
> This works fine at the command line (where the folder "c:\aa bb" exists)
> 
>  > os.rename( "c\aa bb", "c:\cc dd" );
>  >
> 
> But, I can't get it to work in my program, eg.
> 
> print SrcDir
> print NewDir
> os.rename( SrcDir, NewDir );
> 
> when I run this I get something like this:
> 
> "e:\\music\\Joni Mitchell\\ogg-8"
> "e:\\music.ogg\\Joni Mitchell\\ogg-8"

What kind of device is drive E: ?  Are you certain it allows spaces in 
filenames?  Can you do the same renaming *using the same drive* at the 
command line or from Explorer?

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-09 Thread Larry Bates
Are you sure the source directory exists and you
have rights to rename it?  Because the rename works
for me.

But you may want to look at shutil.move and/or
use forward slashes (they work under Windows)

-Larry Bates


Tom wrote:
> I'm having a problem using a path with spaces as a parameter to
> os.rename() in a program on WinXP.
> 
> This works fine at the command line (where the folder "c:\aa bb" exists)
> 
>> os.rename( "c\aa bb", "c:\cc dd" );
>>
> 
> But, I can't get it to work in my program, eg.
> 
> print SrcDir
> print NewDir
> os.rename( SrcDir, NewDir );
> 
> when I run this I get something like this:
> 
> "e:\\music\\Joni Mitchell\\ogg-8"
> "e:\\music.ogg\\Joni Mitchell\\ogg-8"
> 
> Traceback (most recent call last):
>   File "E:\Music\MoveMusic.py", line 64, in ?
> main();
> ...
>   File "E:\Music\MoveMusic.py", line 49, in Visit
> os.mkdir( NewDir );
> OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni
> Mitchell\\ogg-8"'
> 
> I've tried different combinations of single backslash vs. double
> backslash, and quoted vs. non-quoted, but it always fails.
> 
> The problem is not specific to os.rename.  If I instead use mkdir(
> SrcDir ) I get the same problem.
> 
> Thanks,
> Tom.
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


simple problem with os.rename() parameters - path with spaces

2005-09-09 Thread Tom
I'm having a problem using a path with spaces as a parameter to 
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)

 > os.rename( "c\aa bb", "c:\cc dd" );
 >

But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"

Traceback (most recent call last):
   File "E:\Music\MoveMusic.py", line 64, in ?
 main();
...
   File "E:\Music\MoveMusic.py", line 49, in Visit
 os.mkdir( NewDir );
OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni 
Mitchell\\ogg-8"'

I've tried different combinations of single backslash vs. double 
backslash, and quoted vs. non-quoted, but it always fails.

The problem is not specific to os.rename.  If I instead use mkdir( 
SrcDir ) I get the same problem.

Thanks,
Tom.

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


Re: Simple Problem

2005-07-24 Thread ncf
Thank you all for your replies. The repr() solution wasn't exactly what
I was looking for, as I wasn't planning on eval()ing it, but the
(en|de)code solution was exactly what I was looking for. An extended
thanks to Jp for informing me of the version compatibility :)

Have a GREAT day :)
-Wes

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


Re: Simple Problem

2005-07-24 Thread Jp Calderone
On 24 Jul 2005 18:14:13 -0700, ncf <[EMAIL PROTECTED]> wrote:
>I know I've seen this somewhere before, but does anyone know what the
>function to escape a string is? (i.e., encoding newline to "\n" and a
>chr(254) to "\xfe") (and visa-versa)
>
>Thanks for helping my ignorance :P

Python 2.4.1 (#2, Mar 30 2005, 21:51:10) 
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '\n\xfe'.encode('string-escape')
'\\n\\xfe'
>>> '\\n\\xfe'.decode('string-escape')
'\n\xfe'
>>> 

  Introduced in Python 2.3

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


Re: Simple Problem

2005-07-24 Thread Robert Kern
ncf wrote:
> I know I've seen this somewhere before, but does anyone know what the
> function to escape a string is? (i.e., encoding newline to "\n" and a
> chr(254) to "\xfe") (and visa-versa)

In [1]: s = "foo\n\xfe"

In [2]: s.encode("string_escape")
Out[2]: 'foo\\n\\xfe'

In [3]: repr(s)[1:-1]
Out[3]: 'foo\\n\\xfe'

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter

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


Re: Simple Problem

2005-07-24 Thread Paul Rubin
"ncf" <[EMAIL PROTECTED]> writes:
> I know I've seen this somewhere before, but does anyone know what the
> function to escape a string is? (i.e., encoding newline to "\n" and a
> chr(254) to "\xfe") (and visa-versa)

repr(s)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Problem

2005-07-24 Thread Cyril Bazin
By any chance are you speaking about the function "repr" ?
Cyril
On 24 Jul 2005 18:14:13 -0700, ncf <[EMAIL PROTECTED]> wrote:
I know I've seen this somewhere before, but does anyone know what thefunction to escape a string is? (i.e., encoding newline to "\n" and achr(254) to "\xfe") (and visa-versa)Thanks for helping my ignorance :P
-Wes--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Simple Problem

2005-07-24 Thread ncf
I know I've seen this somewhere before, but does anyone know what the
function to escape a string is? (i.e., encoding newline to "\n" and a
chr(254) to "\xfe") (and visa-versa)

Thanks for helping my ignorance :P

-Wes

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