Re: None shown in output

2012-06-21 Thread Xander Solis
Thanks for replying still! Appreciate the help.

On Fri, Jun 22, 2012 at 12:25 PM, Benjamin Kaplan
wrote:

> damn
>
> On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan
>  wrote:
> > On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis  wrote:
> >> Hello Python list,
> >>
> >> Noob here with a newbie question. I'm reading and working on the
> exercise of
> >> the book, Learn Python the Hard way 2.0. When I use this code, I get
> "None"
> >> on the output. My question is why does this happen?
> >>
> >> def get_numbers(first_num, second_num, operator):
> >>
> >> if operator == 'add':
> >> print first_num + second_num
> >> elif operator == 'minus':
> >> print first_num - second_num
> >> elif operator == 'divide':
> >> print first_num / second_num
> >> elif operator == 'multiply':
> >> print first_num * second_num
> >>
> >> print "%r" % (get_numbers(1, 2, 'minus'))
> >> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> >> print "%r" % (get_numbers(10, 2, 'divide'))
> >>
> >> Output:
> >>
> >> C:\code\python>ex19.py
> >> -1
> >> None
> >> 15
> >> None
> >> 5
> >> None
> >> 7.5
> >>
> >> --
> >>
> >> Thanks in advance for your help.
> >>
> >> Regards,
> >>
> >> Xander
> >>
> >
> > printing something just writes the value to th
>
> I hate when I accidentally hit send early. Anyway, Michael already
> gave you the reason- print and return two different things.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Terry Reedy

On 6/21/2012 11:42 PM, Xander Solis wrote:

Hello Python list,

Noob here with a newbie question. I'm reading and working on the
exercise of the book, Learn Python the Hard way 2.0. When I use this
code, I get "None" on the output. My question is why does this happen?


None is the default return value for python-coded functions and python 
prints the value of expressions in interactive mode. Hmmm. But you seem 
to be running in batch mode. But where is 7.5 from. You must be doing 
more than shown.


Regardless, in my opinion, instead of printing, the function should 
return the computed value. You can then print the returned value. Real 
software should generally work that way. If nothing else, so you can 
automate tests instead of checking screen output.



def get_numbers(first_num, second_num, operator):

 if operator == 'add':
 print first_num + second_num
 elif operator == 'minus':
 print first_num - second_num
 elif operator == 'divide':
 print first_num / second_num
 elif operator == 'multiply':
 print first_num * second_num

print "%r" % (get_numbers(1, 2, 'minus'))
print "%r" % (get_numbers(1+3, 2+9, 'add'))
print "%r" % (get_numbers(10, 2, 'divide'))

Output:

C:\code\python>ex19.py
-1
None
15
None
5
None
7.5


--
Terry Jan Reedy



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


Re: None shown in output

2012-06-21 Thread Xander Solis
Thanks Andrew and Michael!. That did the trick.

def get_numbers(first_num, second_num, operator):

if operator == 'add':
value = first_num + second_num
elif operator == 'minus':
value = first_num - second_num
elif operator == 'divide':
value = first_num / second_num
elif operator == 'multiply':
value = first_num * second_num

return value

print "%r" % (get_numbers(1, 2, 'minus'))
print "%r" % (get_numbers(1+3, 2+9, 'add'))
print "%r" % (get_numbers(10, 2, 'divide'))

output:

-1
15
5

On Fri, Jun 22, 2012 at 12:23 PM, Andrew Berg wrote:

> On 6/21/2012 10:42 PM, Xander Solis wrote:
> > Hello Python list,
> >
> > Noob here with a newbie question. I'm reading and working on the
> > exercise of the book, Learn Python the Hard way 2.0. When I use this
> > code, I get "None" on the output. My question is why does this happen?
> Your function prints the number and then returns None (you have no
> return statement in the function to change this) to the print statement.
> If you want to have the function return a value, use the return
> statement instead of print inside the function:
>
> def func(some_value):
>  return some_value
> x = func(5)
> print x
>
> will print 5.
>
> --
> CPython 3.3.0a4 | Windows NT 6.1.7601.17803
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Benjamin Kaplan
damn

On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan
 wrote:
> On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis  wrote:
>> Hello Python list,
>>
>> Noob here with a newbie question. I'm reading and working on the exercise of
>> the book, Learn Python the Hard way 2.0. When I use this code, I get "None"
>> on the output. My question is why does this happen?
>>
>> def get_numbers(first_num, second_num, operator):
>>
>>     if operator == 'add':
>>         print first_num + second_num
>>     elif operator == 'minus':
>>         print first_num - second_num
>>     elif operator == 'divide':
>>         print first_num / second_num
>>     elif operator == 'multiply':
>>         print first_num * second_num
>>
>> print "%r" % (get_numbers(1, 2, 'minus'))
>> print "%r" % (get_numbers(1+3, 2+9, 'add'))
>> print "%r" % (get_numbers(10, 2, 'divide'))
>>
>> Output:
>>
>> C:\code\python>ex19.py
>> -1
>> None
>> 15
>> None
>> 5
>> None
>> 7.5
>>
>> --
>>
>> Thanks in advance for your help.
>>
>> Regards,
>>
>> Xander
>>
>
> printing something just writes the value to th

I hate when I accidentally hit send early. Anyway, Michael already
gave you the reason- print and return two different things.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Dan Sommers
On Fri, 22 Jun 2012 11:42:28 +0800
Xander Solis  wrote:

> Noob here with a newbie question. I'm reading and working on the
> exercise of the book, Learn Python the Hard way 2.0. When I use this
> code, I get "None" on the output. My question is why does this happen?
> 
> def get_numbers(first_num, second_num, operator):
> 
> if operator == 'add':
> print first_num + second_num
> elif operator == 'minus':
> print first_num - second_num
> elif operator == 'divide':
> print first_num / second_num
> elif operator == 'multiply':
> print first_num * second_num

This function prints a result, but doesn't return anything.

> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))

This:

(get_numbers(1, 2, 'minus'))

calls get_numbers and evaluates to None (because get_numbers doesn't
return anything).

Then this:

"%r" % (get_numbers(1, 2, 'minus'))

formats that None into the string "None."

Finally, this:

print "%r" % (get_numbers(1, 2, 'minus'))

prints that string.

> Output:
> 
> C:\code\python>ex19.py
> -1

This "-1" comes from get_numbers.

> None

This "None" comes from the print statement that called get_numbers.

Contrast your code with this snippet:

def add(x, y):
return x + y

print "%r" % (add(3, 2))

HTH,
Dan

-- 
Μὴ μοῦ τοὺς κύκλους τάραττε -- Αρχιμηδησ
Do not disturb my circles. -- Archimedes

Dan Sommers, http://www.tombstonezero.net/dan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Benjamin Kaplan
On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis  wrote:
> Hello Python list,
>
> Noob here with a newbie question. I'm reading and working on the exercise of
> the book, Learn Python the Hard way 2.0. When I use this code, I get "None"
> on the output. My question is why does this happen?
>
> def get_numbers(first_num, second_num, operator):
>
>     if operator == 'add':
>         print first_num + second_num
>     elif operator == 'minus':
>         print first_num - second_num
>     elif operator == 'divide':
>         print first_num / second_num
>     elif operator == 'multiply':
>         print first_num * second_num
>
> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))
>
> Output:
>
> C:\code\python>ex19.py
> -1
> None
> 15
> None
> 5
> None
> 7.5
>
> --
>
> Thanks in advance for your help.
>
> Regards,
>
> Xander
>

printing something just writes the value to th
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Andrew Berg
On 6/21/2012 10:42 PM, Xander Solis wrote:
> Hello Python list,
> 
> Noob here with a newbie question. I'm reading and working on the
> exercise of the book, Learn Python the Hard way 2.0. When I use this
> code, I get "None" on the output. My question is why does this happen?
Your function prints the number and then returns None (you have no
return statement in the function to change this) to the print statement.
If you want to have the function return a value, use the return
statement instead of print inside the function:

def func(some_value):
  return some_value
x = func(5)
print x

will print 5.

-- 
CPython 3.3.0a4 | Windows NT 6.1.7601.17803
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None shown in output

2012-06-21 Thread Michael Hrivnak
The last three lines print the return value from the "get_numbers"
function, which isn't returning anything.  In python, the default
return value is None, and that's why you're seeing it.

Michael

On Thu, Jun 21, 2012 at 11:42 PM, Xander Solis  wrote:
> Hello Python list,
>
> Noob here with a newbie question. I'm reading and working on the exercise of
> the book, Learn Python the Hard way 2.0. When I use this code, I get "None"
> on the output. My question is why does this happen?
>
> def get_numbers(first_num, second_num, operator):
>
>     if operator == 'add':
>         print first_num + second_num
>     elif operator == 'minus':
>         print first_num - second_num
>     elif operator == 'divide':
>         print first_num / second_num
>     elif operator == 'multiply':
>         print first_num * second_num
>
> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))
>
> Output:
>
> C:\code\python>ex19.py
> -1
> None
> 15
> None
> 5
> None
> 7.5
>
> --
>
> Thanks in advance for your help.
>
> Regards,
>
> Xander
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


None shown in output

2012-06-21 Thread Xander Solis
Hello Python list,

Noob here with a newbie question. I'm reading and working on the exercise
of the book, Learn Python the Hard way 2.0. When I use this code, I get
"None" on the output. My question is why does this happen?

def get_numbers(first_num, second_num, operator):

if operator == 'add':
print first_num + second_num
elif operator == 'minus':
print first_num - second_num
elif operator == 'divide':
print first_num / second_num
elif operator == 'multiply':
print first_num * second_num

print "%r" % (get_numbers(1, 2, 'minus'))
print "%r" % (get_numbers(1+3, 2+9, 'add'))
print "%r" % (get_numbers(10, 2, 'divide'))

Output:

C:\code\python>ex19.py
-1
None
15
None
5
None
7.5

--

Thanks in advance for your help.

Regards,

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


Re: bdist_wininst [was: Custom build of Python]

2012-06-21 Thread Mark Hammond

On 22/06/2012 3:10 AM, KACVINSKY Tom wrote:

I found what I was looking for:

 python setup.py bdist_wininst


bdist_wininst is for creating installers for Python packages which 
install into an existing Python directory structure.  It isn't used to 
create a installer for Python itself (which unless I misunderstand, 
seems what you are trying to do).


Apart from generating an "official" MSI like Python itself ships as, 
IIUC, there is no other way to take a built tree and create an 
installer.  On the other hand though, a built tree will generally work 
fine if transplanted somewhere else, so long as a couple of registry 
entries are created.  So you could do something like creating an inno 
installer script that takes your built tree and generates a custom 
installer for your build.


Mark



But... I follow all of the instructions for building Python on Windows and then 
follow the instructions for using bdist_wininst, and I get this:

C:\Users\tky\Python\Python-2.6.8>PCbuild\amd64\python.exe setup.py bdist_wininst
running bdist_wininst
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
Traceback (most recent call last):
   File "setup.py", line 2049, in 
 main()
   File "setup.py", line 2044, in main
 'Lib/smtpd.py']
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\core.py", line 152, in 
setup
 dist.run_commands()
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 975, in 
run_commands
 self.run_command(cmd)
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
 cmd_obj.run()
   File 
"C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\bdist_wininst.py", line 
125, in run
 self.run_command('build')
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\cmd.py", line 333, in 
run_command
 self.distribution.run_command(command)
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
 cmd_obj.run()
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\build.py", line 
134, in run
 self.run_command(cmd_name)
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\cmd.py", line 333, in 
run_command
 self.distribution.run_command(command)
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
 cmd_obj.run()
   File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\build_ext.py", 
line 340, in run
 self.build_extensions()
   File "setup.py", line 167, in build_extensions
 raise ValueError("No source directory; cannot proceed.")
ValueError: No source directory; cannot proceed.

Does anyone know of a way to get bdist_wininst to work out of the box?   I am 
not comfortable with hacking distutils.  What do the maintainers do in this 
case?

Thanks,

Tom

-Original Message-
From: python-list-bounces+tky=3ds@python.org 
[mailto:python-list-bounces+tky=3ds@python.org] On Behalf Of KACVINSKY Tom
Sent: Wednesday, June 20, 2012 4:29 PM
To: python-list@python.org
Subject: RE: Custom build of Python

Terry,

At this stage, I don't want or need an MSI.  I just want something that will 
bundle the executables/dynamic load libraries + compiled Python files and stick 
them into a compliant directory structure.

Regards,

Tom
-Original Message-
From: python-list-bounces+tky=3ds@python.org 
[mailto:python-list-bounces+tky=3ds@python.org] On Behalf Of Terry Reedy
Sent: Wednesday, June 20, 2012 4:13 PM
To: python-list@python.org
Subject: Re: Custom build of Python

On 6/20/2012 2:24 PM, KACVINSKY Tom wrote:

I had reason to build Python 2.6.8 with Microsoft Visual Studio 2010.
I was able to get the pcbuild solution to build, and I have the
necessary exes/dlls/pyds in the amd64 build directory.  What is not
clear to is how to complete the build and make an installation.  I
could not find any documentation for this.


If you mean 'make a .msi file', I am not sure that is not officially supported 
beyond the inclusion of msilib.


Any help on this matter would be appreciated.


3.3 is now being built with VS2010. Perhaps its repository has something that 
will help you.

--
Terry Jan Reedy



--
http://mail.python.org/mailman/listinfo/python-list
This email and any attachments are intended solely for the use of the 
individual or entity to whom it is addressed and may be confidential and/or 
privileged.

If you are not one of the named recipients or have received this email in error,

(i) you should not read, disclose, or copy it,

(ii) please notify sender of your receipt by reply email and delete this email 
and all attachments,

(iii) Dassault Systemes does not accept or assume any liability or 
responsibility for any use of or reliance on this email.

For other languages, go to http://www.3ds.com/terms/email-disclaimer
--
http://mail.python.org/mailman/listinfo/python-list
This email and any attachments are intended solely for the use of the 
individual or

Re: inno setup 5.5

2012-06-21 Thread inq1ltd
On Friday, June 22, 2012 08:30:16 AM Chris Angelico wrote:
> On Fri, Jun 22, 2012 at 1:34 AM, inq1ltd  wrote:
> > Can't find _thinter
> 
> Is this supposed to be _tkinter?
> 
> Try copying and pasting the entire error message, including traceback.
> That often helps. At the moment, I can't know whether the error is in
> your transcription of the message or if in a source file somewhere (a
> misspelled import, for instance).
> 
> Chris Angelico


Thanks for responding,

The following is as it is in the exe text doc.

Traceback (most recent call last):
  File "INQVISTA.py", line 23, in 
  File "INQVISTA.py", line 19, in __init__
  File "INQVISTA_0.pyc", line 23, in __init__
  File "INQVISTA_0.pyc", line 75, in __init__
  File "INQVISTA_0.pyc", line 78, in FmakeBox
  File "Tkinter.pyc", line 1643, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories: 
{C:/Program Files/lib/tcl8.5} {C:/Program Files/lib/tcl8.5} C:/lib/tcl8.5 
{C:/Program Files/library} C:/library C:/tcl8.5.2/library C:/tcl8.5.2/library


This probably means that Tcl wasn't installed properly.

 //

the line of code that the error msg refers to is:

Mwelc =  Tk () 
 
jd







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


Re: Help me with a bytes decoding problem in python 3

2012-06-21 Thread Chris Angelico
On Fri, Jun 22, 2012 at 8:30 AM, J  wrote:
> I'm wondering now... the way it works is that the program is run from
> a user terminal/console.  however, on the other side (after the reboot
> is done) it runs via an autostart script after the user is logged in,
> and thus runs outside of the terminal.  So I wonder if THAT could be
> the root cause of this headache...  maybe there's something about the
> program being run outside of a user shell and generating those
> characters that causes the hang.

It may be a difference in the default encoding of STDIN/STDOUT. When
you're running in a terminal, they're probably being set to UTF-8, so
Python's reading and writing will be in Unicode; possibly it's set to
ASCII

import sys
print(sys.stdin.encoding)

There are many potential differences between TTY access and whatever
happens on startup. How is your script being invoked? Is it through
Upstart? I know you can arrange where stdio go in Upstart scripts.

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


Re: Help me with a bytes decoding problem in python 3

2012-06-21 Thread J
On Thu, Jun 21, 2012 at 5:54 PM, Chris Angelico  wrote:
> On Fri, Jun 22, 2012 at 7:47 AM, J  wrote:
>> \xe2\x86\xb3
>
> This is the UTF-8 encoded form of U+21B3, which is the DOWNWARDS ARROW
> WITH TIP RIGHTWARDS character that you're showing. That's what the
> "bytecode" you're seeing is. You may be able to ask the underlying
> program to make its output in a cleaner format - try looking for a
> --porcelain option or similar - but as I see it, everything in Python
> is doing what it ought to do.
>
> Or am I misunderstanding your question?
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list

No, that's exactly what was choking my python program...

And you hit the nail on the head.  Apparently, if I run xinput using
default output (what we were doing originally) you get those
characters.  If you run it like so:

xinput list --name-only

you get a clean, text only list of only the names of the various input
devices found.  So doing that, after an afternoon of pulling my hair
out over this mess, makes the program work as it's supposed to.

What confuses me though, is that the program calls that function
twice, and this works fine the first time around, but fails the
second.  The function is called before rebooting the machine, then is
called again after rebooting the machine and compares the output of
various system info commands to make sure no hardware disappeared
after a reboot.

This was not an issue in Python 2.x, it's only on the Python 3
conversion this occurred. :/ In fact, if I change the shebang from
python3 back to python2.7, it works beautifully.

In the log file, this appears the first time that the xinput command is run:
2012-06-21 18:02:43,043 INFO Gathering hardware information...
2012-06-21 18:02:43,043 DEBUGExecuting: 'xinput list'...
2012-06-21 18:02:43,052 DEBUGOutput:
- returncode:
0
- stdout:
⎡ Virtual core pointer  id=2[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointerid=4[slave  pointer  (2)]
⎜   ↳ Logitech Unifying Device. Wireless PID:1017   id=9[slave
 pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPadid=12   [slave  pointer  (2)]
⎜   ↳ MCE IR Keyboard/Mouse (ite-cir)   id=14   [slave  pointer  (2)]
⎣ Virtual core keyboard id=3[master keyboard (2)]
↳ Virtual core XTEST keyboard   id=5[slave  keyboard (3)]
↳ Power Button  id=6[slave  keyboard (3)]
↳ Video Bus id=7[slave  keyboard (3)]
↳ Laptop_Integrated_Webcam_2M   id=8[slave  keyboard (3)]
↳ HID 413c:8157 id=10   [slave  keyboard (3)]
↳ AT Translated Set 2 keyboard  id=11   [slave  keyboard (3)]
↳ Dell WMI hotkeys  id=13   [slave  keyboard (3)]
↳ ITE8708 CIR transceiver   id=15   [slave  keyboard (3)]

2012-06-21 18:02:43,053 DEBUGTouchpad and Keyboard:
⎡ Virtual core pointer  id=2[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointerid=4[slave  pointer  (2)]
⎜   ↳ Logitech Unifying Device. Wireless PID:1017   id=9[slave
 pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPadid=12   [slave  pointer  (2)]
⎜   ↳ MCE IR Keyboard/Mouse (ite-cir)   id=14   [slave  pointer  (2)]
⎣ Virtual core keyboard id=3[master keyboard (2)]
↳ Virtual core XTEST keyboard   id=5[slave  keyboard (3)]
↳ Power Button  id=6[slave  keyboard (3)]
↳ Video Bus id=7[slave  keyboard (3)]
↳ Laptop_Integrated_Webcam_2M   id=8[slave  keyboard (3)]
↳ HID 413c:8157 id=10   [slave  keyboard (3)]
↳ AT Translated Set 2 keyboard  id=11   [slave  keyboard (3)]
↳ Dell WMI hotkeys  id=13   [slave  keyboard (3)]
↳ ITE8708 CIR transceiver   id=15   [slave  keyboard (3)]

(it appears twice because I ran it in debug mode)

but after the reboot, it chokes:

2012-06-21 18:03:56,396 INFO reboot operations remaining: 0
2012-06-21 18:03:56,396 INFO Reboot time: 0:01:13.396280
2012-06-21 18:04:27,201 INFO Gathering hardware information...
2012-06-21 18:04:27,201 DEBUGExecuting: 'xinput list'...
2012-06-21 18:07:22,985 DEBUGRemoving desktop file
('/home/bladernr/.config/autostart/pm_test.desktop')...
2012-06-21 18:07:22,986 DEBUGRestoring sudoers configuration...
2012-06-21 18:07:22,986 DEBUGExecuting: "sed -i -e '/#
Automatically added by pm.py/,+1d' /etc/sudoers"...
2012-06-21 18:07:22,993 DEBUGRestoring autologin configuration...
2012-06-21 18:07:23,013 INFO Reboot test cancelled by user

Everything after "Executing: 'xinput list'..." is where I manually
cancelled the run because it stuck.


Re: inno setup 5.5

2012-06-21 Thread Chris Angelico
On Fri, Jun 22, 2012 at 1:34 AM, inq1ltd  wrote:
> Can't find _thinter

Is this supposed to be _tkinter?

Try copying and pasting the entire error message, including traceback.
That often helps. At the moment, I can't know whether the error is in
your transcription of the message or if in a source file somewhere (a
misspelled import, for instance).

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


Re: [newbie] Equivalent to PHP?

2012-06-21 Thread Gilles
On Thu, 21 Jun 2012 10:44:08 -0700, Paul Rubin
 wrote:
>There are both kinds.  The first kind is called a Virtual Private Server
>(VPS).  The second kind is called shared hosting.

Thanks much for the infos.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help me with a bytes decoding problem in python 3

2012-06-21 Thread Chris Angelico
On Fri, Jun 22, 2012 at 7:47 AM, J  wrote:
> \xe2\x86\xb3

This is the UTF-8 encoded form of U+21B3, which is the DOWNWARDS ARROW
WITH TIP RIGHTWARDS character that you're showing. That's what the
"bytecode" you're seeing is. You may be able to ask the underlying
program to make its output in a cleaner format - try looking for a
--porcelain option or similar - but as I see it, everything in Python
is doing what it ought to do.

Or am I misunderstanding your question?

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


Help me with a bytes decoding problem in python 3

2012-06-21 Thread J
I have a bit of a problem I hope you could help me sort out with some
code I'm porting from 2.x to 3.

I have a program with a wrapper for Popen that is called to run
certain linux shell commands and gather system info.  Essentially the
code looks something like this:


process = 
subprocess.Popen(command_str,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
result = process.communicate()

stdout,stderr = result

message = ['Output:\n'
  '- returncode:\n{0}'.format(self.process.returncode)]
if stdout:
   if type(stdout) is bytes:
   stdout = stdout.decode()
   message.append('- stdout:\n{0}'.format(stdout))
if stderr:
   if type(stderr) is bytes:
   stderr = stderr.decode()
   message.append('- stderr:\n{0}'.format(stderr))
logging.debug('\n'.join(message))

So what essentially happens is that command_str is passed to this
function and is a string like this:

'lspci | grep Network'
or
'xinput list'

using the first command string, I get output like this:

In [12]: command = 'lspci |grep Network'
In [13]: process =
subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
In [14]: result = process.communicate()
In [15]: stdout,stderr = result
In [16]: result
Out[16]:
(b'00:19.0 Ethernet controller: Intel Corporation 82577LC Gigabit
Network Connection (rev 06)\n07:00.0 Network controller: Intel
Corporation Ultimate N WiFi Link 5300\n',
 b'')

Which is a bytes object that is very easily converted to text by the
decode() call prior to sending "message" to the logger.

However, the program seems to now be hanging up when running the
second command_str because it's actually returning bytecode inside the
bytes object:

In [18]: command = 'xinput list'
In [19]: process =
subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
In [20]: result = process.communicate()
In [21]: stdout,stderr = result
In [22]: result
Out[22]:
(b'\xe2\x8e\xa1 Virtual core pointer
\tid=2\t[master pointer  (3)]\n\xe2\x8e\x9c   \xe2\x86\xb3 Virtual
core XTEST pointer  \tid=4\t[slave  pointer
(2)]\n\xe2\x8e\x9c   \xe2\x86\xb3 Logitech Unifying Device. Wireless
PID:1017\tid=9\t[slave  pointer  (2)]\n\xe2\x8e\x9c   \xe2\x86\xb3
SynPS/2 Synaptics TouchPad  \tid=12\t[slave  pointer
(2)]\n\xe2\x8e\x9c   \xe2\x86\xb3 MCE IR Keyboard/Mouse (ite-cir)
  \tid=14\t[slave  pointer  (2)]\n\xe2\x8e\xa3 Virtual core keyboard
\tid=3\t[master keyboard (2)]\n\xe2\x86\xb3
Virtual core XTEST keyboard \tid=5\t[slave  keyboard
(3)]\n\xe2\x86\xb3 Power Button
\tid=6\t[slave  keyboard (3)]\n\xe2\x86\xb3 Video Bus
\tid=7\t[slave  keyboard (3)]\n\xe2\x86\xb3
Laptop_Integrated_Webcam_2M \tid=8\t[slave  keyboard
(3)]\n\xe2\x86\xb3 HID 413c:8157
\tid=10\t[slave  keyboard (3)]\n\xe2\x86\xb3 AT Translated Set 2
keyboard\tid=11\t[slave  keyboard (3)]\n\xe2\x86\xb3
Dell WMI hotkeys\tid=13\t[slave  keyboard
(3)]\n\xe2\x86\xb3 ITE8708 CIR transceiver
\tid=15\t[slave  keyboard (3)]\n',
 b'')

Unfortunately, what's happening here is all that extra formatting
stuff that xinput injects is also converted when doing bytes.decode()
on result[0].


In [24]: result[0].decode()
Out[24]: '⎡ Virtual core pointer\tid=2\t[master
pointer  (3)]\n⎜   ↳ Virtual core XTEST pointer
\tid=4\t[slave  pointer  (2)]\n⎜   ↳ Logitech Unifying Device.
Wireless PID:1017\tid=9\t[slave  pointer  (2)]\n⎜   ↳ SynPS/2
Synaptics TouchPad  \tid=12\t[slave  pointer  (2)]\n⎜   ↳
MCE IR Keyboard/Mouse (ite-cir) \tid=14\t[slave  pointer
(2)]\n⎣ Virtual core keyboard   \tid=3\t[master
keyboard (2)]\n↳ Virtual core XTEST keyboard
\tid=5\t[slave  keyboard (3)]\n↳ Power Button
 \tid=6\t[slave  keyboard (3)]\n↳ Video Bus
   \tid=7\t[slave  keyboard (3)]\n↳
Laptop_Integrated_Webcam_2M \tid=8\t[slave  keyboard
(3)]\n↳ HID 413c:8157   \tid=10\t[slave
keyboard (3)]\n↳ AT Translated Set 2 keyboard
\tid=11\t[slave  keyboard (3)]\n↳ Dell WMI hotkeys
  \tid=13\t[slave  keyboard (3)]\n↳ ITE8708 CIR transceiver
 \tid=15\t[slave  keyboard (3)]\n'

And so I believe that when that decoded string is being pushed to the
logger, the logger is choking, causing the program to just hang
waiting on something... not sure which.

Though when doing this manually via ipython3, logger seems to handle
it just fine:

In [37]: message = ['Output:\n'
'- returncode:\n{0}'.format(process.returncode)]

In [38]: message.append('- stdout:\n{0}'.format(stdout.decode()))

In [39]: logging.debug('\n'.join(message))
DEBUG:root:Output:
- returncode:
0
- stdout:
⎡ Virtual core pointer  id=2[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointerid=4[slave  pointer  (2)]
⎜   ↳ Logitech Unifying Device. Wireless PID:1017   id=9   

[ANNOUNCE] greenlet 0.4.0

2012-06-21 Thread Ralf Schmitt
Hi,

I have uploaded greenlet 0.4.0 to PyPI:
http://pypi.python.org/pypi/greenlet

What is it?
---
The greenlet module provides coroutines for python. coroutines allow
suspending and resuming execution at certain locations.

concurrence[1], eventlet[2] and gevent[3] use the greenlet module in
order to implement concurrent network applications.

Documentation can be found here: http://greenlet.readthedocs.org

The code is hosted on github:
https://github.com/python-greenlet/greenlet


Changes in version 0.4.0

The NEWS file lists these changes for release 0.4.0:

* Greenlet has an instance dictionary now, which means it can be
  used for implementing greenlet local storage, etc. However, this
  might introduce incompatibility if subclasses have __dict__ in their
  __slots__. Classes like that will fail, because greenlet already
  has __dict__ out of the box.
* Greenlet no longer leaks memory after thread termination, as long as
  terminated thread has no running greenlets left at the time.
* Add support for debian sparc and openbsd5-sparc64
* Add support for ppc64 linux
* Don't allow greenlets to be copied with copy.copy/deepcopy
* Fix arm32/thumb support
* Restore greenlet's parent after kill
* Add experimental greenlet tracing


Many thanks to Alexey Borzenkov, who spent a lot of time on this release
and everyone else that helped make this release happen.


[1] http://opensource.hyves.org/concurrence/
[2] http://eventlet.net/
[3] http://www.gevent.org/

-- 
Cheers
Ralf Schmitt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to compare below 2 json structure in python

2012-06-21 Thread MRAB

On 21/06/2012 19:16, hisan wrote:

sample_json1={{
"globalControlId": 72,
"value": 0,
"controlId": 2
},
{
"globalControlId": 77,
"value": 3,
"controlId": 7
}
}

sample_json2={
{
"globalControlId": 77,
"value": 3,
"controlId": 7
},
 {
"globalControlId": 72,
"value": 0,
"controlId": 2
}
}


That doesn't look like valid JSON.

The only container types in JSON are list (delimited by "[" and "]")
and dict (delimited by "{" and "}").
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to compare below 2 json structure in python

2012-06-21 Thread John Gordon
In  hisan 
 writes:

> sample_json1={{
>"globalControlId": 72,
>"value": 0,
>"controlId": 2
>},
>{
>"globalControlId": 77,
>"value": 3,
>"controlId": 7
>}
> }

> sample_json2={
>{
>"globalControlId": 77,
>"value": 3,
>"controlId": 7
>},
> {
>"globalControlId": 72,
>"value": 0,
>"controlId": 2
>}
> }

Assuming you have valid json strings (which these aren't), I think you
could convert them into python objects with json.loads() and then compare
the python objects.

For example:

>>> import json
>>> json1 = '{"color": "blue", "amount": 10}'
>>> json2 = '{"amount": 10, "color": "blue"}'
>>> python1 = json.loads(json1)
>>> python2 = json.loads(json2)
>>> print python1 == python2
True
>>>

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Strange threading behaviour

2012-06-21 Thread Dave Angel
On 06/21/2012 02:03 PM, Rotwang wrote:
> On 21/06/2012 18:07, Dave Angel wrote:
>> On 06/21/2012 11:19 AM, Rotwang wrote:
>>> Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written
>>> is acting strangely. I can reproduce the behaviour in question with
>>> the following:
>>>
>>> --- begin bugtest.py ---
>>>
>>> import threading, Tkinter, os, pickle
>>>
>>> class savethread(threading.Thread):
>>>  def __init__(self, value):
>>>  threading.Thread.__init__(self)
>>>  self.value = value
>>>  def run(self):
>>>  print 'Saving:',
>>>  with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
>>>  pickle.dump(self.value, f)
>>>  print 'saved'
>>>
>>> class myclass(object):
>>>  def gui(self):
>>>  root = Tkinter.Tk()
>>>  root.grid()
>>>  def save(event):
>>>  savethread(self).start()
>>>  root.bind('s', save)
>>>  root.wait_window()
>>>
>>> m = myclass()
>>> m.gui()
>>>
>>> --- end bugtest.py ---
>>>
>>>
>>> Here's the problem: suppose I fire up Python and type
>>>
>> import bugtest
>>>
>>> and then click on the Tk window that spawns and press 's'. Then
>>> 'Saving:' gets printed, and an empty file named 'bugfile' appears in
>>> my current working directory. But nothing else happens until I close
>>> the Tk window; as soon as I do so the file is written to and 'saved'
>>> gets printed. If I subsequently type
>>>
>> bugtest.m.gui()
>>>
>>> and then click on the resulting window and press 's', then 'Saving:
>>> saved' gets printed and the file is written to immediately, exactly as
>>> I would expect. Similarly if I remove the call to m.gui from the
>>> module and just call it myself after importing then it all works fine.
>>> But it seems as if calling the gui within the module itself somehow
>>> stops savethread(self).run from finishing its job while the gui is
>>> still alive.
>>>
>>> Can anyone help?
>>>
>>>
>>
>> I did not study your code, as I'm not very familiar with tkinter.
>> However, I think I know your problem:
>>
>> You do not want to try to start up threads from within a import.  An
>> import is special, and somehow blocks threading while it's running.
>
> That would explain it.
>
>
>> Consequently, a module should not try to do anything too fancy from
>> within its top-level code.  Add in the traditional:
>>
>> def main():
>>  m = myclass()
>>  m.gui()
>>
>> if __name__ == "__main__":
>>  main()
>>
>> and just run it from the command line, aspython bugtest.py
>
> In fact, running the module directly from the command line makes it
> work properly as-is.

But if you leave it as-is, then you can't run it from the interactive
interpreter.

>
> Thanks for your post.
>

just for completeness, see link; 
http://docs.python.org/library/threading.html

" ... an import should not have the side effect of spawning a new thread
and then waiting for that thread in any way..."


-- 

DaveA


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


Re: Strange threading behaviour

2012-06-21 Thread Rotwang

On 21/06/2012 18:37, Dennis Lee Bieber wrote:

On Thu, 21 Jun 2012 16:19:41 +0100, Rotwang
declaimed the following in gmane.comp.python.general:



import threading, Tkinter, os, pickle

class savethread(threading.Thread):
  def __init__(self, value):
  threading.Thread.__init__(self)
  self.value = value
  def run(self):
  print 'Saving:',
  with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
  pickle.dump(self.value, f)
  print 'saved'

class myclass(object):
  def gui(self):
  root = Tkinter.Tk()
  root.grid()
  def save(event):
  savethread(self).start()
  root.bind('s', save)


Each time your "s" runs you are creating a NEW thread.


Yes, that's the whole point: I want the object's state to be saved in 
the background while I can continue using the GUI without having to wait 
(in the original module where I noticed the problem, the thread pickles 
a copy of the object whose gui method created it, rather than the object 
itself - that way I can make changes to the object without interfering 
with the work being done by pickle.dump).






  root.wait_window()

m = myclass()
m.gui()

--- end bugtest.py ---


Here's the problem: suppose I fire up Python and type

  >>>  import bugtest


Running in the interpreter rather than as a program...


and then click on the Tk window that spawns and press 's'. Then
'Saving:' gets printed, and an empty file named 'bugfile' appears in my
current working directory. But nothing else happens until I close the Tk
window; as soon as I do so the file is written to and 'saved' gets
printed. If I subsequently type

  >>>  bugtest.m.gui()

and then click on the resulting window and press 's', then 'Saving:
saved' gets printed and the file is written to immediately, exactly as I
would expect. Similarly if I remove the call to m.gui from the module
and just call it myself after importing then it all works fine. But it
seems as if calling the gui within the module itself somehow stops
savethread(self).run from finishing its job while the gui is still alive.


Since you never kill the previous thread, you might have a binding
to left-over multiple threads; at the least, the Python scheduler may be
swapping between threads.


Which previous thread do you mean? The problem happens when save has 
only been called once.




The task swapping probably gives the threads time to flush output.
With just the first run thread condition, the process may go back to the
Tk mainloop and not return to the thread to completely flush data.

Especially with the use of .wait_window(), which is supposed to lock
the application until the window is destroyed. If overly aggressive, it
might even lock up swapping back to the thread to allow it to flush.


Can anyone help?


Don't run from the interactive interpreter?


Thanks.

--
Hate music? Then you'll hate this:

http://tinyurl.com/psymix
--
http://mail.python.org/mailman/listinfo/python-list


how to compare below 2 json structure in python

2012-06-21 Thread hisan
sample_json1={{
   "globalControlId": 72,
   "value": 0,
   "controlId": 2
   },
   {
   "globalControlId": 77,
   "value": 3,
   "controlId": 7
   }
}

sample_json2={
   {
   "globalControlId": 77,
   "value": 3,
   "controlId": 7
   },
{
   "globalControlId": 72,
   "value": 0,
   "controlId": 2
   }
}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange threading behaviour

2012-06-21 Thread Rotwang

On 21/06/2012 18:07, Dave Angel wrote:

On 06/21/2012 11:19 AM, Rotwang wrote:

Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written
is acting strangely. I can reproduce the behaviour in question with
the following:

--- begin bugtest.py ---

import threading, Tkinter, os, pickle

class savethread(threading.Thread):
 def __init__(self, value):
 threading.Thread.__init__(self)
 self.value = value
 def run(self):
 print 'Saving:',
 with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
 pickle.dump(self.value, f)
 print 'saved'

class myclass(object):
 def gui(self):
 root = Tkinter.Tk()
 root.grid()
 def save(event):
 savethread(self).start()
 root.bind('s', save)
 root.wait_window()

m = myclass()
m.gui()

--- end bugtest.py ---


Here's the problem: suppose I fire up Python and type


import bugtest


and then click on the Tk window that spawns and press 's'. Then
'Saving:' gets printed, and an empty file named 'bugfile' appears in
my current working directory. But nothing else happens until I close
the Tk window; as soon as I do so the file is written to and 'saved'
gets printed. If I subsequently type


bugtest.m.gui()


and then click on the resulting window and press 's', then 'Saving:
saved' gets printed and the file is written to immediately, exactly as
I would expect. Similarly if I remove the call to m.gui from the
module and just call it myself after importing then it all works fine.
But it seems as if calling the gui within the module itself somehow
stops savethread(self).run from finishing its job while the gui is
still alive.

Can anyone help?




I did not study your code, as I'm not very familiar with tkinter.
However, I think I know your problem:

You do not want to try to start up threads from within a import.  An
import is special, and somehow blocks threading while it's running.


That would explain it.



Consequently, a module should not try to do anything too fancy from
within its top-level code.  Add in the traditional:

def main():
 m = myclass()
 m.gui()

if __name__ == "__main__":
 main()

and just run it from the command line, aspython bugtest.py


In fact, running the module directly from the command line makes it work 
properly as-is.


Thanks for your post.

--
Hate music? Then you'll hate this:

http://tinyurl.com/psymix
--
http://mail.python.org/mailman/listinfo/python-list


bdist_wininst [was: Custom build of Python]

2012-06-21 Thread KACVINSKY Tom
I found what I was looking for:

python setup.py bdist_wininst

But... I follow all of the instructions for building Python on Windows and then 
follow the instructions for using bdist_wininst, and I get this:

C:\Users\tky\Python\Python-2.6.8>PCbuild\amd64\python.exe setup.py bdist_wininst
running bdist_wininst
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
Traceback (most recent call last):
  File "setup.py", line 2049, in 
main()
  File "setup.py", line 2044, in main
'Lib/smtpd.py']
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\core.py", line 152, in 
setup
dist.run_commands()
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 975, in 
run_commands
self.run_command(cmd)
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
cmd_obj.run()
  File 
"C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\bdist_wininst.py", line 
125, in run
self.run_command('build')
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\cmd.py", line 333, in 
run_command
self.distribution.run_command(command)
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
cmd_obj.run()
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\build.py", line 
134, in run
self.run_command(cmd_name)
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\cmd.py", line 333, in 
run_command
self.distribution.run_command(command)
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\dist.py", line 995, in 
run_command
cmd_obj.run()
  File "C:\Users\tky\Python\Python-2.6.8\lib\distutils\command\build_ext.py", 
line 340, in run
self.build_extensions()
  File "setup.py", line 167, in build_extensions
raise ValueError("No source directory; cannot proceed.")
ValueError: No source directory; cannot proceed.

Does anyone know of a way to get bdist_wininst to work out of the box?   I am 
not comfortable with hacking distutils.  What do the maintainers do in this 
case?

Thanks,

Tom

-Original Message-
From: python-list-bounces+tky=3ds@python.org 
[mailto:python-list-bounces+tky=3ds@python.org] On Behalf Of KACVINSKY Tom
Sent: Wednesday, June 20, 2012 4:29 PM
To: python-list@python.org
Subject: RE: Custom build of Python

Terry,

At this stage, I don't want or need an MSI.  I just want something that will 
bundle the executables/dynamic load libraries + compiled Python files and stick 
them into a compliant directory structure.

Regards,

Tom
-Original Message-
From: python-list-bounces+tky=3ds@python.org 
[mailto:python-list-bounces+tky=3ds@python.org] On Behalf Of Terry Reedy
Sent: Wednesday, June 20, 2012 4:13 PM
To: python-list@python.org
Subject: Re: Custom build of Python

On 6/20/2012 2:24 PM, KACVINSKY Tom wrote:
> I had reason to build Python 2.6.8 with Microsoft Visual Studio 2010.
> I was able to get the pcbuild solution to build, and I have the
> necessary exes/dlls/pyds in the amd64 build directory.  What is not
> clear to is how to complete the build and make an installation.  I
> could not find any documentation for this.

If you mean 'make a .msi file', I am not sure that is not officially supported 
beyond the inclusion of msilib.

> Any help on this matter would be appreciated.

3.3 is now being built with VS2010. Perhaps its repository has something that 
will help you.

--
Terry Jan Reedy



--
http://mail.python.org/mailman/listinfo/python-list
This email and any attachments are intended solely for the use of the 
individual or entity to whom it is addressed and may be confidential and/or 
privileged.

If you are not one of the named recipients or have received this email in error,

(i) you should not read, disclose, or copy it,

(ii) please notify sender of your receipt by reply email and delete this email 
and all attachments,

(iii) Dassault Systemes does not accept or assume any liability or 
responsibility for any use of or reliance on this email.

For other languages, go to http://www.3ds.com/terms/email-disclaimer
--
http://mail.python.org/mailman/listinfo/python-list
This email and any attachments are intended solely for the use of the 
individual or entity to whom it is addressed and may be confidential and/or 
privileged.

If you are not one of the named recipients or have received this email in error,

(i) you should not read, disclose, or copy it,

(ii) please notify sender of your receipt by reply email and delete this email 
and all attachments,

(iii) Dassault Systemes does not accept or assume any liability or 
responsibility for any use of or reliance on this email.

For other languages, go to http://www.3ds.com/terms/email-disclaimer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Equivalent to PHP?

2012-06-21 Thread Paul Rubin
Gilles  writes:
> Do Python hosters provide a VM so that it's just like a remote Linux
> server where I'm free to install whatever I want, or do they force
> users to use specific versions of Python and specific frameworks eg.
> Django?

There are both kinds.  The first kind is called a Virtual Private Server
(VPS).  The second kind is called shared hosting.  VPS is very flexible
but shared hosting can be cheaper, and easier to use if you have no
sysadmin skills.  I'm assuming you don't have enough workload to want to
rent a physical server (sometimes called a "dedi", short for dedicated
server) or colo (colocation, meaning you buy your own hardware and
install it in a rack slot that you rent at the data center).


OVH (www.ovh.com/fr) is the biggest French hosting place and they have
all three types of service, I think.  Their low cost branch is
www.kimsufi.fr which has extremely attractive prices.  I have heard
mixed things about them: you get a lot of resources for the money, but
that they can be hard to deal with in various ways.  So it sounds not
great but not terrible.  Kimsufi dedis are cheaper than anything
comparable that we can get in the USA, so I have been interested in
renting one for some of my own stuff, but I haven't pursued this yet.

www.webhostingtalk.com and www.lowendbox.com are good places to research
this sort of thing.  lowendbox.com is specifically for cheap VPS's.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange threading behaviour

2012-06-21 Thread inq1ltd
On Thursday, June 21, 2012 11:46:30 AM inq1ltd wrote:
> On Thursday, June 21, 2012 04:19:41 PM Rotwang wrote:
> > Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written is
> > acting strangely. I can reproduce the behaviour in question with the
> > following:
> > 
> > --- begin bugtest.py ---
> > 
> > import threading, Tkinter, os, pickle
>

correct my last response to this,
 
try this;
 
from Tkinter import *
 
import threading, Tkinter, os, pickle

it works for me.




my module included

if __name__ == '__main__' :
print '##open bugtest , 29\n'
myclass()

so I could run 
python bugtest.py 
in a shell

 
 jd
> 
> > class savethread(threading.Thread):
> >  def __init__(self, value):
> >  threading.Thread.__init__(self)
> >  self.value = value
> >  
> >  def run(self):
> >  print 'Saving:',
> >  
> >  with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
> >  pickle.dump(self.value, f)
> >  
> >  print 'saved'
> > 
> > class myclass(object):
> >  def gui(self):
> >  root = Tkinter.Tk()
> >  root.grid()
> >  
> >  def save(event):
> >  savethread(self).start()
> >  
> >  root.bind('s', save)
> >  root.wait_window()
> > 
> > m = myclass()
> > m.gui()
> > 
> > --- end bugtest.py ---
> > 
> > 
> > Here's the problem: suppose I fire up Python and type
> > 
> >  >>> import bugtest
> > 
> > and then click on the Tk window that spawns and press 's'. Then
> > 'Saving:' gets printed, and an empty file named 'bugfile' appears in my
> > current working directory. But nothing else happens until I close the Tk
> > window; as soon as I do so the file is written to and 'saved' gets
> > printed. If I subsequently type
> > 
> >  >>> bugtest.m.gui()
> > 
> > and then click on the resulting window and press 's', then 'Saving:
> > saved' gets printed and the file is written to immediately, exactly as I
> > would expect. Similarly if I remove the call to m.gui from the module
> > and just call it myself after importing then it all works fine. But it
> > seems as if calling the gui within the module itself somehow stops
> > savethread(self).run from finishing its job while the gui is still
> > alive.
> > 
> > Can anyone help?-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange threading behaviour

2012-06-21 Thread Dieter Maurer
Rotwang  writes:

> Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written
> is acting strangely. I can reproduce the behaviour in question with
> the following:
>
> --- begin bugtest.py ---
>
> import threading, Tkinter, os, pickle
>
> class savethread(threading.Thread):
> def __init__(self, value):
> threading.Thread.__init__(self)
> self.value = value
> def run(self):
> print 'Saving:',
> with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
> pickle.dump(self.value, f)
> print 'saved'
>
> class myclass(object):
> def gui(self):
> root = Tkinter.Tk()
> root.grid()
> def save(event):
> savethread(self).start()
> root.bind('s', save)
> root.wait_window()
>
> m = myclass()
> m.gui()
>
> --- end bugtest.py ---
>
>
> Here's the problem: suppose I fire up Python and type
>
 import bugtest
>
> and then click on the Tk window that spawns and press 's'. Then
> 'Saving:' gets printed, and an empty file named 'bugfile' appears in
> my current working directory. But nothing else happens until I close
> the Tk window; as soon as I do so the file is written to and 'saved'
> gets printed. If I subsequently type
>
 bugtest.m.gui()
>
> and then click on the resulting window and press 's', then 'Saving:
> saved' gets printed and the file is written to immediately, exactly as
> I would expect. Similarly if I remove the call to m.gui from the
> module and just call it myself after importing then it all works
> fine. But it seems as if calling the gui within the module itself
> somehow stops savethread(self).run from finishing its job while the
> gui is still alive.
>
> Can anyone help?

It looks as if some waiting operation
in the "wait_window" call did not release the GIL (the "Global Interpreter
Lock" which ensures that at most one Python thread can run at a given
time and protects the Python data structures such as the reference counts
and interpreter state).

In this case, you could expect some non-deterministic behaviour.
If your thread is fast enough to finish before the internal
activity inside "wait_window" gets the GIL again,
everything completes immediately; otherwise, things complete
only after the internal waiting ends and Python code is again executed.


It might well be possible that "TKinter" has not been designed for
a multi threaded environment; alternatively, there might be a bug.
If "TKinter" truely supports multithreaded applications, any call
to "tk" would need to release the GIL and any callback into Python
reacquire it. Strange things of the kind you observe could happen
when this is forgotten at a single place.

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


Re: Strange threading behaviour

2012-06-21 Thread Temia Eszteri
On Thu, 21 Jun 2012 10:12:07 -0700, Temia Eszteri
 wrote:
>
>Try appending the dump command with f.flush() and os.fsync().
>
>~Temia

Actually, wait, no. The behavior you're describing is indicating that
the thread in question isn't even getting a chance to execute at all.
I'd recommend going with Dave's idea then, see how it pans out.

Still, a good set of commands to keep in mind.

~Temia
-- 
The amazing programming device: fuelled entirely by coffee, it codes while
awake and tests while asleep!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange threading behaviour

2012-06-21 Thread Temia Eszteri
On Thu, 21 Jun 2012 16:19:41 +0100, Rotwang 
wrote:

>Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written is 
>acting strangely. I can reproduce the behaviour in question with the 
>following:
>
>--- begin bugtest.py ---
>
>import threading, Tkinter, os, pickle
>
>class savethread(threading.Thread):
> def __init__(self, value):
> threading.Thread.__init__(self)
> self.value = value
> def run(self):
> print 'Saving:',
> with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
> pickle.dump(self.value, f)
> print 'saved'
>
>class myclass(object):
> def gui(self):
> root = Tkinter.Tk()
> root.grid()
> def save(event):
> savethread(self).start()
> root.bind('s', save)
> root.wait_window()
>
>m = myclass()
>m.gui()
>
>--- end bugtest.py ---
>
>
>Here's the problem: suppose I fire up Python and type
>
> >>> import bugtest
>
>and then click on the Tk window that spawns and press 's'. Then 
>'Saving:' gets printed, and an empty file named 'bugfile' appears in my 
>current working directory. But nothing else happens until I close the Tk 
>window; as soon as I do so the file is written to and 'saved' gets 
>printed. If I subsequently type
>
> >>> bugtest.m.gui()
>
>and then click on the resulting window and press 's', then 'Saving: 
>saved' gets printed and the file is written to immediately, exactly as I 
>would expect. Similarly if I remove the call to m.gui from the module 
>and just call it myself after importing then it all works fine. But it 
>seems as if calling the gui within the module itself somehow stops 
>savethread(self).run from finishing its job while the gui is still alive.
>
>Can anyone help?

Try appending the dump command with f.flush() and os.fsync().

~Temia
-- 
The amazing programming device: fuelled entirely by coffee, it codes while
awake and tests while asleep!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange threading behaviour

2012-06-21 Thread Dave Angel
On 06/21/2012 11:19 AM, Rotwang wrote:
> Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written
> is acting strangely. I can reproduce the behaviour in question with
> the following:
>
> --- begin bugtest.py ---
>
> import threading, Tkinter, os, pickle
>
> class savethread(threading.Thread):
> def __init__(self, value):
> threading.Thread.__init__(self)
> self.value = value
> def run(self):
> print 'Saving:',
> with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
> pickle.dump(self.value, f)
> print 'saved'
>
> class myclass(object):
> def gui(self):
> root = Tkinter.Tk()
> root.grid()
> def save(event):
> savethread(self).start()
> root.bind('s', save)
> root.wait_window()
>
> m = myclass()
> m.gui()
>
> --- end bugtest.py ---
>
>
> Here's the problem: suppose I fire up Python and type
>
> >>> import bugtest
>
> and then click on the Tk window that spawns and press 's'. Then
> 'Saving:' gets printed, and an empty file named 'bugfile' appears in
> my current working directory. But nothing else happens until I close
> the Tk window; as soon as I do so the file is written to and 'saved'
> gets printed. If I subsequently type
>
> >>> bugtest.m.gui()
>
> and then click on the resulting window and press 's', then 'Saving:
> saved' gets printed and the file is written to immediately, exactly as
> I would expect. Similarly if I remove the call to m.gui from the
> module and just call it myself after importing then it all works fine.
> But it seems as if calling the gui within the module itself somehow
> stops savethread(self).run from finishing its job while the gui is
> still alive.
>
> Can anyone help?
>
>

I did not study your code, as I'm not very familiar with tkinter.
However, I think I know your problem:

You do not want to try to start up threads from within a import.  An
import is special, and somehow blocks threading while it's running.

Consequently, a module should not try to do anything too fancy from
within its top-level code.  Add in the traditional:

def main():
m = myclass()
m.gui()

if __name__ == "__main__":
main()

and just run it from the command line, aspython bugtest.py

And if you want to run it from a interactive python session, do the call
to main() after importing it:

 import bugtest
 bugtest.main()


-- 

DaveA

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


Re: Strange threading behaviour

2012-06-21 Thread inq1ltd
On Thursday, June 21, 2012 04:19:41 PM Rotwang wrote:
> Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written is
> acting strangely. I can reproduce the behaviour in question with the
> following:
> 
> --- begin bugtest.py ---
> 
> import threading, Tkinter, os, pickle

try this;

from Tkinter import *

take it out of the other import line

jd 



> 
> class savethread(threading.Thread):
>  def __init__(self, value):
>  threading.Thread.__init__(self)
>  self.value = value
>  def run(self):
>  print 'Saving:',
>  with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
>  pickle.dump(self.value, f)
>  print 'saved'
> 
> class myclass(object):
>  def gui(self):
>  root = Tkinter.Tk()
>  root.grid()
>  def save(event):
>  savethread(self).start()
>  root.bind('s', save)
>  root.wait_window()
> 
> m = myclass()
> m.gui()
> 
> --- end bugtest.py ---
> 
> 
> Here's the problem: suppose I fire up Python and type
> 
>  >>> import bugtest
> 
> and then click on the Tk window that spawns and press 's'. Then
> 'Saving:' gets printed, and an empty file named 'bugfile' appears in my
> current working directory. But nothing else happens until I close the Tk
> window; as soon as I do so the file is written to and 'saved' gets
> printed. If I subsequently type
> 
>  >>> bugtest.m.gui()
> 
> and then click on the resulting window and press 's', then 'Saving:
> saved' gets printed and the file is written to immediately, exactly as I
> would expect. Similarly if I remove the call to m.gui from the module
> and just call it myself after importing then it all works fine. But it
> seems as if calling the gui within the module itself somehow stops
> savethread(self).run from finishing its job while the gui is still alive.
> 
> Can anyone help?-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generator vs functools.partial?

2012-06-21 Thread J. Cliff Dyer
On Thu, 2012-06-21 at 21:25 +1000, John O'Hagan wrote:
> Sometimes a function gets called repeatedly with the same expensive argument:
> 
> def some_func(arg, i):
> (do_something with arg and i)
> 
> same_old_arg = big_calculation()
> for i in lots_of_items:
> some_func(same_old_arg, i)
> 

Another possibility is to refactor this into a callable class.

class DoesSomethingWithExpensiveData(object):
def __init__(self, arg):
self.arg = arg

def __call__(self, operand):
return sum([self.arg, operand])

func = DoesSomethingWithExpensiveData(big_calculation()):
for thing in a_bunch_of_things:
print func(thing)

Or you can write a function factory:

def get_func(arg):
def inner(operand):
return sum([arg, operand])
return inner

func = get_func(big_calculation())
for thing in a_bunch_of_things:
print func(thing)


> A simple case like that looks OK, but it can get messy when groups of 
> arguments
> are passed between functions, especially if the arguments are used by 
> functions
> called within the functions they are passed to (by other functions!).
> 
> Maybe that's a code smell, but it can be cleaned up with:
> 
> import functools
> some_func = functools.partial(some_func, big_calculation())
> for i in lots_of_items:
> some_func(i)
> 
> But what about a generator?
> 
> def some_func():
> arg = big_calculation()
> while 1:
> i = yield
> (do_something with arg and i)
> 
> some_gen = some_func()
> some_gen.send(None)
> for i in lots_of_items:
> some_gen.send(i)
> 
> I like this because it encapsulates the calculation of the arguments
> inside the function that is using them without repeating it, and there are no
> restrictions on argument order like partial. But sending None is annoying. :)
> 
> Old news? Thoughts, criticisms, theories?
> 
> --
> 
> John  


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


inno setup 5.5

2012-06-21 Thread inq1ltd
py help,

Can someone let me know where to get 
help with inno setup.

I get an error;
Can't find _thinter

Sounds like a path problem.

Everything works until it is packaged with
inno v5.5.

jd


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


Parse command line output using textfsm

2012-06-21 Thread Ferdinand Sousa
Hi List,

I am using python to run command line utilities on Linux machine.
I came across textfsm and feel that it fits my requirements.

Here is the structure of the output from the 2 utilities

Command 1:

Id AddressPort Location State  Tenant count Max tenants Description
-- --   --  --- 
1  10.0.32.20 8443 -ACTIVE712   -
2  10.0.32.21 8443 -ACTIVE712   -
3  10.0.32.22 8443 -ACTIVE712   -

Command 2:

12012-04-04 19:54 00DD000q3di GOLDCompany 1   CA360
ACTIVE  Yes   https://10.0.32.26:7000 8 50
17  2012-04-09 23:01 00Dd000efRF SILVER  Company 2   CA360
ACTIVE  Yes   https://10.0.32.20:7014 1 440
27  2012-04-10 03:42 00Dd000efdS TRIAL Company 3   CA360
ACTIVE  Yes   https://10.0.32.23:7021 4 10
32  2012-04-11 11:24 00Dd000eiGb TRIAL Company 4  CA360
INACTIVE   Yes   https://10.0.32.21:7000 2 25

I found this link documenting how to parse tabular data:
http://code.google.com/p/textfsm/wiki/TextFSMCodeLab#Parsing_tabular_data
My question is, what is the syntax of the pattern matching used. Is it the
same as python regexp?
Any other documentation/examples available for this library?

I'm also open to suggestions of using other libraries.
Basically, I would like this to be semantically as close as possible to
working with data extracted from a database.

Thanks and regards,
Ferdi
-- 
http://mail.python.org/mailman/listinfo/python-list


Strange threading behaviour

2012-06-21 Thread Rotwang
Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written is 
acting strangely. I can reproduce the behaviour in question with the 
following:


--- begin bugtest.py ---

import threading, Tkinter, os, pickle

class savethread(threading.Thread):
def __init__(self, value):
threading.Thread.__init__(self)
self.value = value
def run(self):
print 'Saving:',
with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
pickle.dump(self.value, f)
print 'saved'

class myclass(object):
def gui(self):
root = Tkinter.Tk()
root.grid()
def save(event):
savethread(self).start()
root.bind('s', save)
root.wait_window()

m = myclass()
m.gui()

--- end bugtest.py ---


Here's the problem: suppose I fire up Python and type

>>> import bugtest

and then click on the Tk window that spawns and press 's'. Then 
'Saving:' gets printed, and an empty file named 'bugfile' appears in my 
current working directory. But nothing else happens until I close the Tk 
window; as soon as I do so the file is written to and 'saved' gets 
printed. If I subsequently type


>>> bugtest.m.gui()

and then click on the resulting window and press 's', then 'Saving: 
saved' gets printed and the file is written to immediately, exactly as I 
would expect. Similarly if I remove the call to m.gui from the module 
and just call it myself after importing then it all works fine. But it 
seems as if calling the gui within the module itself somehow stops 
savethread(self).run from finishing its job while the gui is still alive.


Can anyone help?


--
Hate music? Then you'll hate this:

http://tinyurl.com/psymix
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generator vs functools.partial?

2012-06-21 Thread John O'Hagan
On Thu, 21 Jun 2012 14:20:23 +0200
Thomas Rachel
 wrote:

> Am 21.06.2012 13:25 schrieb John O'Hagan:
> 
> > But what about a generator?
> 
> Yes, but...
> 
> > def some_func():
> >  arg = big_calculation()
> >  while 1:
> >  i = yield
> >  (do_something with arg and i)
> >
> > some_gen = some_func()
> > some_gen.send(None)
> > for i in lots_of_items:
> >  some_gen.send(i)
> 
> rather
> 
> def some_func(it):
>  arg = big_calculation()
>  for i in it:
>  do_something(arg, i)
> 
> some_func(lots_of_items)
> 
Ah yes, this demonstrates that my examples were too simple to show what I'm
trying to do. In my real code, several functions take the elements from an
iterator, so I can't pass the whole iterator to each one. I have a feeling there
is some bad factoring in my real code which will disappear if I manage to
provide a clear example, which I'll now endevour to do. Thanks for the reply.

--

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


Re: Generator vs functools.partial?

2012-06-21 Thread John O'Hagan
On 21 Jun 2012 12:19:20 GMT
Steven D'Aprano  wrote:

> On Thu, 21 Jun 2012 21:25:04 +1000, John O'Hagan wrote:
> 
> > Sometimes a function gets called repeatedly with the same expensive
> > argument:
> > 
> > def some_func(arg, i):
> > (do_something with arg and i)
> > 
> > same_old_arg = big_calculation()
> 
> Since big_calculation() is only called once, the cost of generating that 
> expensive argument is only paid once.
> 
> > for i in lots_of_items:
> > some_func(same_old_arg, i)
> 
> Passing that same_old_arg to some_func is cheap. Once you've paid the 
> cost of producing the value in the first place, there is absolutely no 
> difference in cost between passing an "expensive" argument and a "cheap" 
> argument.
> 
>  
> > A simple case like that looks OK, but it can get messy when groups of
> > arguments are passed between functions, especially if the arguments are
> > used by functions called within the functions they are passed to (by
> > other functions!).
> 
> I'm not sure what you're trying to say here. Argument passing is cheap. 
> Function call overhead is not quite so cheap, but unless you've carefully 
> profiled your code and determined that the cost of function overhead is 
> significant, you're better off using ignoring it. Likely the actual 
> calculation within the function is hundreds or thousands of times more 
> expensive than the function call itself.
[...]

What I neglected to say was that I'm looking to refactor for clarity and DRY
to minimise the number of objects that get passed through a chain of functions
before being used, rather than efficiency. I realise the different examples I
gave (and the more obvious one in Thomas's reply) do more or less the same
thing and that there would probably only be marginal efficiency differences.
I'll try to come up with better examples to show what I mean. Or not: see my
reply to Thomas.

> 
> Premature optimization is the root of all evil.
> 

100% agreement.

Thanks,
--

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


Re: Introspect imports from module

2012-06-21 Thread Bastian Ballmann
Hi,

that's really great stuff! I love it! Thx :)


Am Thu, 21 Jun 2012 13:00:36 +0200
schrieb Christian Heimes :

> Am 21.06.2012 10:03, schrieb Bastian Ballmann:
> > Any suggestions how I could just get the import of
> > module.to.inspect? Thanks && have a nice day!
> 
> You could try a completely different approach and use the compiler
> package to inspect the abstract syntrax tree of a compiled module. The
> approach has the nice side effect that you don't have to import a
> module to inspect its imports. This script may serve as an example
> for you:
> 
> http://svn.zope.org/Zope3/trunk/utilities/importchecker.py?rev=113742&view=auto
> 
> Christian
> 



-- 
Bastian Ballmann / Web Developer
Notch Interactive GmbH / Badenerstrasse 571 / 8048 Zürich
Phone Phone +41 44 297 17 17 / www.notch-interactive.com

CHECK OUR LATEST WORK: http://www.notch-interactive.com/projekte/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fastest method

2012-06-21 Thread Jean-Michel Pichavant

david.gar...@gmail.com wrote:

I am looking for the fastest way to parse a log file.


currently I have this... Can I speed this up any? The script is 
written to be a generic log file parser so I can't rely on some 
predictable pattern.



def check_data(data,keywords):
#get rid of duplicates
unique_list = list(set(data))
string_list=' '.join(unique_list)
#print string_list
for keyword in keywords:
if keyword in string_list:
return True


I am currently using file seek and maintaining a last byte count file:

with open(filename) as f:
print "Here is filename:%s" %filename
f.seek(0, 2)
eof = f.tell()
print "Here is eof:%s" %eof
if last is not None:
print "Here is last:%s" %last
# if last is less than current
last = int(last)
if (eof - last  > 0):
offset = eof - last
offset = offset * -1
print "Here is new offset:%s" %offset
f.seek(offset, 2)
mylist = f.readlines()
else:
# if last doesn't exist or is greater than current
f.seek(0)
bof = f.tell()
print "Here is bof:%s" %bof
mylist = f.readlines()



Thanks,
--
David Garvey

I have a log parser that take action upon some log patterns.
I rely on my system 'grep' program to do the hard work, i.e. find 
occurrences



Of course that means it is system dependant, but I don't think you can 
beat grep's speed.


   def _grep(self, link, pattern):
   # return the number of occurences for the pattern in the file
   proc = subprocess.Popen(['grep', '-c', pattern, link], 
stdout=subprocess.PIPE)

   return int(proc.communicate()[0])

Cheers,

JM

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


Re: Generator vs functools.partial?

2012-06-21 Thread Thomas Rachel

Am 21.06.2012 13:25 schrieb John O'Hagan:


But what about a generator?


Yes, but...


def some_func():
 arg = big_calculation()
 while 1:
 i = yield
 (do_something with arg and i)

some_gen = some_func()
some_gen.send(None)
for i in lots_of_items:
 some_gen.send(i)


rather

def some_func(it):
arg = big_calculation()
for i in it:
do_something(arg, i)

some_func(lots_of_items)


HTH,

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


Re: [newbie] Equivalent to PHP?

2012-06-21 Thread Gilles
On Tue, 12 Jun 2012 11:39:50 +0200, Gilles  wrote:
>I'm an amateur programmer, and would like to know what the main
>options are to build web applications in Python instead of PHP.

When I need to host my Python application (preferably in Europe since
my users will be located there), what are the options?

Do Python hosters provide a VM so that it's just like a remote Linux
server where I'm free to install whatever I want, or do they force
users to use specific versions of Python and specific frameworks eg.
Django?

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


Re: Generator vs functools.partial?

2012-06-21 Thread Steven D'Aprano
On Thu, 21 Jun 2012 21:25:04 +1000, John O'Hagan wrote:

> Sometimes a function gets called repeatedly with the same expensive
> argument:
> 
> def some_func(arg, i):
> (do_something with arg and i)
> 
> same_old_arg = big_calculation()

Since big_calculation() is only called once, the cost of generating that 
expensive argument is only paid once.

> for i in lots_of_items:
> some_func(same_old_arg, i)

Passing that same_old_arg to some_func is cheap. Once you've paid the 
cost of producing the value in the first place, there is absolutely no 
difference in cost between passing an "expensive" argument and a "cheap" 
argument.

 
> A simple case like that looks OK, but it can get messy when groups of
> arguments are passed between functions, especially if the arguments are
> used by functions called within the functions they are passed to (by
> other functions!).

I'm not sure what you're trying to say here. Argument passing is cheap. 
Function call overhead is not quite so cheap, but unless you've carefully 
profiled your code and determined that the cost of function overhead is 
significant, you're better off using ignoring it. Likely the actual 
calculation within the function is hundreds or thousands of times more 
expensive than the function call itself.


> Maybe that's a code smell, but it can be cleaned up with:
> 
> import functools
> some_func = functools.partial(some_func, big_calculation()) 
> for i in lots_of_items:
> some_func(i)

Have you actually measured this to see whether it *actually is* faster, 
or are you just assuming it will be faster?

I expect that at best it will be no faster at all, and quite possibly 
slightly slower. The main reason is that instead of one function call 
(calling some_func directly with two arguments) you now have two (partial 
function called with one argument, which internally calls some_func with 
two). Possibly that internal call is implemented in C and may be a little 
faster than if it were implemented in Python bytecode, but still, two 
calls can't be faster than one.


[...]
> Old news? Thoughts, criticisms, theories?

Premature optimization is the root of all evil.



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


Generator vs functools.partial?

2012-06-21 Thread John O'Hagan
Sometimes a function gets called repeatedly with the same expensive argument:

def some_func(arg, i):
(do_something with arg and i)

same_old_arg = big_calculation()
for i in lots_of_items:
some_func(same_old_arg, i)

A simple case like that looks OK, but it can get messy when groups of arguments
are passed between functions, especially if the arguments are used by functions
called within the functions they are passed to (by other functions!).

Maybe that's a code smell, but it can be cleaned up with:

import functools
some_func = functools.partial(some_func, big_calculation())
for i in lots_of_items:
some_func(i)

But what about a generator?

def some_func():
arg = big_calculation()
while 1:
i = yield
(do_something with arg and i)

some_gen = some_func()
some_gen.send(None)
for i in lots_of_items:
some_gen.send(i)

I like this because it encapsulates the calculation of the arguments
inside the function that is using them without repeating it, and there are no
restrictions on argument order like partial. But sending None is annoying. :)

Old news? Thoughts, criticisms, theories?

--

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


Re: Introspect imports from module

2012-06-21 Thread Christian Heimes
Am 21.06.2012 10:03, schrieb Bastian Ballmann:
> Any suggestions how I could just get the import of module.to.inspect?
> Thanks && have a nice day!

You could try a completely different approach and use the compiler
package to inspect the abstract syntrax tree of a compiled module. The
approach has the nice side effect that you don't have to import a module
to inspect its imports. This script may serve as an example for you:

http://svn.zope.org/Zope3/trunk/utilities/importchecker.py?rev=113742&view=auto

Christian

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


using SQLalchemy

2012-06-21 Thread andrea crotti
We have a very chaotic database (on MySql) at the moment, with for
example table names used as keys to query other tables (but that's just
an example).

We are going to redesign it but first I still have to replace the
perl+vbscript system with only one Python program, so I still have to
deal with the current state.

I'm trying to use SQLalchemy and it looks absolutely great, but in
general as a policy we don't use external dependencies..

To try to do an exception in this case:
- are there any problems with SQLalchemy on Windows?
- are there any possibly drawbacks of using SQLalchemy instead of the
  MySqlDB interface?

  For the second point I guess that we might have a bit less fine
  tuning, but the amount of data is not so much and speed is also not a
  bit issue (also because all the queries are extremely inefficient
  now).

  Any other possible issue?

Thanks,
Andrea
-- 
http://mail.python.org/mailman/listinfo/python-list


Introspect imports from module

2012-06-21 Thread Bastian Ballmann
Hi all,

I am trying to write a function that returns a list of imports a given
module is doing. The "problem" is I dont want to get the imports of the
imports, but that's the case with my current solution.

import __builtin__
old_import = __builtin__.__import__

def import_hook(name, globals=None, locals=None, fromlist=None):
if fromlist:
for symbol in fromlist:
print name + "." + symbol
else:
print name

return old_import(name, globals, locals, fromlist)

__builtin__.__import__ = import_hook
import module.to.inspect

Any suggestions how I could just get the import of module.to.inspect?
Thanks && have a nice day!

Basti
-- 
Bastian Ballmann / Web Developer
Notch Interactive GmbH / Badenerstrasse 571 / 8048 Zürich
Phone Phone +41 44 297 17 17 / www.notch-interactive.com

CHECK OUR LATEST WORK: http://www.notch-interactive.com/projekte/
-- 
http://mail.python.org/mailman/listinfo/python-list