Re: when to use import statements in the header, when to use import statements in the blocks where they are used?

2012-02-08 Thread Mark Lawrence

On 08/02/2012 02:41, Dave Angel wrote:

You forgot to include the list in your reply, so I'm forwarding it for
you. One way you could have done it was to reply-all.


On 02/07/2012 09:32 PM, Patto wrote:

Dave Angel:

On Wed, Feb 8, 2012 at 10:05 AM, Dave Angel wrote:


On 02/07/2012 08:48 PM, Lei Cheng wrote:


Hi all,

In a py file, when to use import statements in the header, when to use
import statements in the blocks where they are used?
What are the best practices?
Thanks!

Pat

Best practice is to put all the imports at the beginning of the module,

so they are easy to spot.

If you put an import inside a function, it gets re-executed each time
the
function is called, which is a waste of time. Not too much, since import
first checks sys.modules to see if it's already loaded.

Also, avoid the from xxx import * form, as it pollutes the
namespace. And it makes it hard to figure out where a particular name is
declared.

I believe these and other best practices can be found in pep8.

http://www.python.org/dev/**peps/pep-0008/


--

DaveA



yeah, I read pep8.
However I find in the file path/to/djcelery/loaders.py from django-celery
source, there are so many import/from statements used inside functions, I
do not know why the author coded like this. Are there any special facts?



I can't speak for django or django-celery. There are people that
disagree on this, and there are some reasons to override the ones I
mentioned. One would be large modules that are not used in most
circumstances, or not used till the program has run for a while.

If you put the import inside a function, you can save on startup time by
deferring some of the logic till later. And if there's a module that
probably won't be used at all (eg. an error handler), perhaps you can
avoid loading it at all.

I still think readability trumps all the other reasons, for nearly all
programs. Only once you decide you have a performance problem should you
change that.



There's a thread on the dev ml about imports that also discusses startup 
times for anyone who's interested.


--
Cheers.

Mark Lawrence.

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


Re: Cycle around a sequence

2012-02-08 Thread Mark Lawrence

On 08/02/2012 01:26, Dennis Lee Bieber wrote:

On Wed, 08 Feb 2012 01:10:28 +, Mark Lawrence
  wrote:


I'm looking at a way of cycling around a sequence i.e. starting at some
given location in the middle of a sequence and running to the end before
coming back to the beginning and running to the start place.  About the
best I could come up with is the following, any better ideas for some
definition of better?

PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
for further copyright information.

from itertools import chain
a=range(10)
g = chain((a[i] for i in xrange(4, 10, 1)), (a[i] for i in xrange(4)))
for x in g: print x,

...
4 5 6 7 8 9 0 1 2 3




How large a sequence and, more important, is it fully known at the
start...


a = range(20)
a

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

a_shift = a[5:] + a[:5]
a_shift

[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]




IOWs, just slice and join: tail first, then front-end.



The sequences are small and the start is always known but the function 
that uses this is called thousands of times so I was trying to avoid 
building lists if at all possible.


--
Cheers.

Mark Lawrence.

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


Re: Cycle around a sequence

2012-02-08 Thread Tim Golden

On 08/02/2012 08:26, Mark Lawrence wrote:

On 08/02/2012 01:26, Dennis Lee Bieber wrote:

On Wed, 08 Feb 2012 01:10:28 +, Mark Lawrence
 wrote:


I'm looking at a way of cycling around a sequence i.e. starting at some
given location in the middle of a sequence and running to the end before
coming back to the beginning and running to the start place. About the
best I could come up with is the following, any better ideas for some
definition of better?

PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
for further copyright information.

from itertools import chain
a=range(10)
g = chain((a[i] for i in xrange(4, 10, 1)), (a[i] for i in
xrange(4)))
for x in g: print x,

...
4 5 6 7 8 9 0 1 2 3




How large a sequence and, more important, is it fully known at the
start...


a = range(20)
a

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

a_shift = a[5:] + a[:5]
a_shift

[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]




IOWs, just slice and join: tail first, then front-end.



The sequences are small and the start is always known but the function
that uses this is called thousands of times so I was trying to avoid
building lists if at all possible.



I'm not an itertools expert, but does this do what you want?
(Untested - I might well be off by one)


import itertools

sequence = range (100)
split = 70
rslice = itertools.islice (sequence, split, len (sequence))
lslice = itertools.islice (sequence, split)

repeater = itertools.cycle (itertools.chain (rslice, lslice))



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


would u wanna details about ur life partner.click here free registration.

2012-02-08 Thread vetri vijay
http://123maza.com/46/river834/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python file synchronization

2012-02-08 Thread Cameron Simpson
[ Please reply inline; it makes the discussion read like a converation,
  with context. - Cameron
]

On 08Feb2012 08:57, Sherif Shehab Aldin  wrote:
| Thanks a lot for your help, I just forgot to state that the FTP server is
| not under my command, I can't control how the file grow, or how the records
| are added, I can only login to It, copy the whole file.

Oh. That's a pity.

| The reason why I am parsing the file and trying to get the diffs between
| the new file and the old one, and copy it to new_file.time_stamp is that I
| need to cut down the file size so when server (C) grabs the file, It grabs
| only new data, also to cut down the network bandwidth.

Can a simple byte count help here? Copy the whole file with FTP. From
the new copy, extract the bytes from the last byte count offset onward.
Then parse the smaller file, extracting whole records for use by (C).
That way you can just keep the unparsed tail (partial record I imagine)
around for the next fetch.

Looking at RFC959 (the FTP protocol):

  http://www.w3.org/Protocols/rfc959/4_FileTransfer.html

it looks like you can do a partial file fetch, also, by issuing a REST
(restart) command to set a file offset and then issuing a RETR (retrieve)
command to get the rest of the file. These all need to be in binary mode
of course.

So in principle you could track the byte offset of what you have fetched
with FTP so far, and fetch only what is new.

| One of my problems was after mounting server (B) diffs_dir into Server (A)
| throw NFS, I used to create filename.lock first into server (B) local file
| then start copy filename to server (B) then remove filename.lock, so when
| the daemon running on server (C) parses the files in the local_diffs dir,
| ignores the files that are still being copied,
| 
| After searching more yesterday, I found that local mv is atomic, so instead
| of creating the lock files, I will copy the new diffs to tmp dir, and after
| the copy is over, mv it to actual diffs dir, that will avoid reading It
| while It's still being copied.

Yes, this sounds good. Provided the mv is on the same filesystem.

For example: "mv /tmp/foo /home/username/foo" is actually a copy and not
a rename because /tmp is normally a different filesystem from /home.

| Sorry if the above is bit confusing, the system is bit complex.

Complex systems often need fiddly solutions.

| Also there is one more factor that confuses me, I am so bad in testing, and
| I am trying to start actually implement unit testing to test my code, what
| I find hard is how to test code like the one that do the copy, mv and so,
| also the code that fetch data from the web.

Ha. I used to be very bad at testing, now I am improving and am merely
weak.

One approach to testing is to make a mock up of the other half of the
system, and test against the mockup.

For example, you have code to FTP new data and then feed it to (C). You
don't control the server side of the FTP. So you might make a small mock
up program that writes valid (but fictitious) data records progressively
to a local data file (write record, flush, pause briefly, etc). If you
can FTP to your own test machine you could then treat _that_ growing
file as the remote server's data file.

Then you could copy it progressively using a byte count to keep track of
the bits you have seen to skip them, and the the

If you can't FTP to your test system, you could abstract out the "fetch
part of this file by FTP" into its own function. Write an equivalent
function that fetches part of a local file just by opening it.

Then you could use the local file version in a test that doesn't
actually do the FTP, but could exercise the rest of it.

It is also useful to make simple tests of small pieces of the code.
So make the code to get part of the data a simple function, and write
tests to execute it in a few ways (no new data, part of a record,
several records etc).

There are many people better than I to teach testing.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Testing can show the presence of bugs, but not their absence.   - Dijkstra
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-08 Thread Case Van Horsen
On Tue, Feb 7, 2012 at 9:37 PM, Alec Taylor  wrote:
> Thanks all for your replies.
>
> I have now installed MSVC8 and YASM.
I assume you installed Visual Studio. I've omitted the commands to use
the SDK compiler below.
>
> I was able to successfully run configure.bat and make.bat (including
> make.bat check).
>
> However, I'm unsure what to do about install, since there is no
> install arg. Do I copy it across to my VC\bin folder, or does it need
> it's own place in PATH + system variables?

The following is just a guess.

I copy the files to a convenient location and then specify that
location to setup.py. Below is an excerpt from my build process.

mkdir c:\src\lib
mkdir c:\src\include
xcopy /Y mpir.h c:\src\include\*.*
xcopy /Y win\mpir.lib c:\src\lib\*.*

python setup.py build_ext -Ic:\src\include -Lc:\src\lib install

>
> I am asking because I don't know where it is looking for the MPIR library.
>
> Thanks for all suggestions,
>
> Alec Taylor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-08 Thread Alec Taylor
Thanks, but to get it to work with pip, wouldn't I need to add it to
PATH? - Or can I just add those library args to pip?

On Wed, Feb 8, 2012 at 9:48 PM, Case Van Horsen  wrote:
> On Tue, Feb 7, 2012 at 9:37 PM, Alec Taylor  wrote:
>> Thanks all for your replies.
>>
>> I have now installed MSVC8 and YASM.
> I assume you installed Visual Studio. I've omitted the commands to use
> the SDK compiler below.
>>
>> I was able to successfully run configure.bat and make.bat (including
>> make.bat check).
>>
>> However, I'm unsure what to do about install, since there is no
>> install arg. Do I copy it across to my VC\bin folder, or does it need
>> it's own place in PATH + system variables?
>
> The following is just a guess.
>
> I copy the files to a convenient location and then specify that
> location to setup.py. Below is an excerpt from my build process.
>
> mkdir c:\src\lib
> mkdir c:\src\include
> xcopy /Y mpir.h c:\src\include\*.*
> xcopy /Y win\mpir.lib c:\src\lib\*.*
>
> python setup.py build_ext -Ic:\src\include -Lc:\src\lib install
>
>>
>> I am asking because I don't know where it is looking for the MPIR library.
>>
>> Thanks for all suggestions,
>>
>> Alec Taylor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert perl-script for voltcraft voltmeter to python [newbie]

2012-02-08 Thread Jean Dupont
On 8 feb, 01:26, Dietmar Schwertberger  wrote:
> Am 03.02.2012 14:11, schrieb Jean Dupont:> As my request might have been too 
> much asked, I have started doing
> > some coding myself.
> > I'm in doubt about the readline statement -which doesn't show anything
> > received- as the meter sends continuously streams of 11 bytes
> > Is there a way to just monitor with python what is arriving at a
> > serial port?
>
> Some time ago I started working on reading data from a VC940.
> I would assume that the protocol is the same.
>
> Please find below the code that will return the raw values from
> a VC940 (tested on a classical RS232 port, but probably
> will work on USB-RS232 converters as well).
>
> If you don't get anything, then you should check whether your
> USB converter is supplying voltage on the DTR pin once you have called
> self.serial.setDTR(1).
>
> You have the description how to decode the values?
> E.g. the string "0003:1401" translates to 0.3 Ohms.
>
> I did not implement anything else, as I just wanted to be sure
> that I could read the values, but I never needed to...
>
> Regards,
>
> Dietmar
>
> import serial
> import time
>
> class VC940(object):
>      def __init__(self, port="COM3"):
>          self.port = port
>          self.serial=serial.Serial(port,2400, bytesize=7, parity="N",
> stopbits=1, timeout=1.5, xonxoff=0, rtscts=0, dsrdtr=None)
>          self.serial.setRTS(0)
>          self.serial.setDTR(0)
>      def _read_raw_value(self):
>          timeout = True
>          for n in range(5):
>              self.serial.flushInput()
>              self.serial.setDTR(1)
>              data = self.serial.read(11)
>              self.serial.setDTR(0)
>              if data.endswith("\r\n") and len(data)==11:
>                  return data
>              if not data:
>                  raise ValueError, "communication timeout"
>          raise ValueError, "could not read data from port"
>
> if __name__=="__main__":
>      vc = VC940()
>      while True:
>          print vc._read_raw_value()

Wow, this is great, it works like a charm. Thanks a lot!

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


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-08 Thread Case Van Horsen
On Wed, Feb 8, 2012 at 4:24 AM, Alec Taylor  wrote:
> Thanks, but to get it to work with pip, wouldn't I need to add it to
> PATH? - Or can I just add those library args to pip?
I don't think so. pyCrypto probably builds a single DLL so the MPIR library is
statically linked into that DLL. Only the innvocation of setup.py should need
to refer to the MPIR library locations.  I don't use pip so I'm not sure how to
get pip to install the resulting DLL, etc.
>
> On Wed, Feb 8, 2012 at 9:48 PM, Case Van Horsen  wrote:
>> On Tue, Feb 7, 2012 at 9:37 PM, Alec Taylor  wrote:
>>> Thanks all for your replies.
>>>
>>> I have now installed MSVC8 and YASM.
>> I assume you installed Visual Studio. I've omitted the commands to use
>> the SDK compiler below.
>>>
>>> I was able to successfully run configure.bat and make.bat (including
>>> make.bat check).
>>>
>>> However, I'm unsure what to do about install, since there is no
>>> install arg. Do I copy it across to my VC\bin folder, or does it need
>>> it's own place in PATH + system variables?
>>
>> The following is just a guess.
>>
>> I copy the files to a convenient location and then specify that
>> location to setup.py. Below is an excerpt from my build process.
>>
>> mkdir c:\src\lib
>> mkdir c:\src\include
>> xcopy /Y mpir.h c:\src\include\*.*
>> xcopy /Y win\mpir.lib c:\src\lib\*.*
>>
>> python setup.py build_ext -Ic:\src\include -Lc:\src\lib install
>>
>>>
>>> I am asking because I don't know where it is looking for the MPIR library.
>>>
>>> Thanks for all suggestions,
>>>
>>> Alec Taylor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cycle around a sequence

2012-02-08 Thread Neil Cerutti
On 2012-02-08, Mark Lawrence  wrote:
> I'm looking at a way of cycling around a sequence i.e. starting
> at some given location in the middle of a sequence and running
> to the end before coming back to the beginning and running to
> the start place.  About the best I could come up with is the
> following, any better ideas for some definition of better?

Python's indices were designed for these kinds of shenanigans.

def rotated(seq, n):
"""Iterate through all of seq, but starting from index n.

>>> ", ".join(str(n) for n in rotated(range(5), 3))
'3, 4, 0, 1, 2'
"""

i = n - len(seq)
while i < n:
yield seq[i]
i += 1

if __name__ == "__main__":
import doctest
doctest.testmod()

If you have merely an iterable instead of a sequence, then look
to some of the other clever stuff already posted.

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



PyDev not saving Ipython History between sessions

2012-02-08 Thread Wanderer
One feature I like about Ipython is that it saves the history between
sessions. The history isn't saved if you close Ipython with the corner
X, but if you type 'exit()' it is saved. This doesn't work when using
Ipython as the console in Pydev. Do I have something setup wrong? Is
there a different command than exit() I should use before closing a
session in PyDev. Is there a command to load the history when I start
a session?

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


Looking for PyPi 2.0...

2012-02-08 Thread Nathan Rice
As a user:
* Finding the right module in PyPi is a pain because there is limited,
low quality semantic information, and there is no code indexing.
* I have to install the module to examine it;  I don't need to look at
docs all the time, sometimes I just want a
package/class/function/method breakdown.
* Given the previous point, having in-line documentation would be nice
(for instance, just the output of .. automodule::)
* Package usage/modification stats are not well exposed
* No code metrics are available
* I would like some kind of social service integration, for tagging
and +1/likes.  I know ratings were scrapped (and they weren't that
useful anyhow), but for example, if Armin Ronacher or Robert Kern
thumbs up a module there is a pretty good chance I will be interested
in it.

As a developer:
* I don't want to have to maintain my code repository and my package
releases separately.  I want to let module repository know that my
code repository exists, and that branches tagged as "release" should
be made available.
* I want to maintain one README.


I don't like "someone needs to do this now" type posts but every time
I use PyPi it infuratiates me.  I usually end up finding modules via
Stack Overflow, which seems silly to me.


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


Naming convention for in-house modules (Newbie question)

2012-02-08 Thread HoneyMonster
I am quite new to Python (2.7 on Linux), and have built a few modules 
using wxPython/wxGlade for GUI elements and Psycopg2 for database access. 
I adhere mostly to the PEP8 guidelines, and use Pylint to help with 
quality control.

So far I have been *very* impressed. Due to Python's straightforwardness 
and the wealth of documentation, results have been excellent.

Here is my question: I would like to start an in-house library of small 
modules to import, for things like error handling/logging. That's easy 
enough, but is there a recommended way of naming such modules? I am 
concerned about avoiding name clashes with standard modules and site 
packages.

Thanks.

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


Re: Looking for PyPi 2.0...

2012-02-08 Thread Amirouche Boubekki
Héllo Nathan,

See below,

2012/2/8 Nathan Rice 

> As a user:
> * Finding the right module in PyPi is a pain because there is limited,
> low quality semantic information, and there is no code indexing.
> * I have to install the module to examine it;  I don't need to look at
> docs all the time, sometimes I just want a
> package/class/function/method breakdown.
> * Given the previous point, having in-line documentation would be nice
> (for instance, just the output of .. automodule::)
> * Package usage/modification stats are not well exposed
> * No code metrics are available
> * I would like some kind of social service integration, for tagging
> and +1/likes.  I know ratings were scrapped (and they weren't that
> useful anyhow), but for example, if Armin Ronacher or Robert Kern
> thumbs up a module there is a pretty good chance I will be interested
> in it.
>
> As a developer:
> * I don't want to have to maintain my code repository and my package
> releases separately.  I want to let module repository know that my
> code repository exists, and that branches tagged as "release" should
> be made available.
> * I want to maintain one README.
>
>
> I don't like "someone needs to do this now" type posts but every time
> I use PyPi it infuratiates me.  I usually end up finding modules via
> Stack Overflow, which seems silly to me.
>
>
Let me do a recap with application analogies:

You want a combination of

   - github / bitbucket for source management *et ce terra*
   - http://readthedocs.org/ for documentation and search
   - http://nullege.com for code search
   - http://repos.io/ for the social features

I think it's not enough for fully integrated project management solution, I
will add this features:

   - Paragraph level and document level comments for documentation like
   http://djangobook.com/
   - Matrix comparaisons : grid feature of http://djangopackages.com/
   - Dependency tracking, library usage in a nice visual way
   - Ask for review thingy
   - Buildbot et all
   - Q/A system
   - Mailling lis
   - Bounties
   - Agenda because time lapse
   - Blogs because it's all the rage
   - CLI interface so that hipsters can hypergrep all the way
   - A big fat red button to promote good libraries into the standard lib
   - A specialized reader UI for hypernerds so that they can cope with that
   much information while still pretending to be human

And when the Python WAR-like format is done, automatic deployement of web
application on the Python.org cloud powered by machines using a pure Python
implemention of the HURD on top a green chips running PyPy.

HTH,

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


Re: Looking for PyPi 2.0...

2012-02-08 Thread Chris Rebert
On Wed, Feb 8, 2012 at 8:54 AM, Nathan Rice
 wrote:
> As a user:
> * Finding the right module in PyPi is a pain because there is limited,
> low quality semantic information, and there is no code indexing.
> * I have to install the module to examine it;  I don't need to look at
> docs all the time, sometimes I just want a
> package/class/function/method breakdown.
> * Given the previous point, having in-line documentation would be nice
> (for instance, just the output of .. automodule::)
> * Package usage/modification stats are not well exposed
> * No code metrics are available
> * I would like some kind of social service integration, for tagging
> and +1/likes.  I know ratings were scrapped (and they weren't that
> useful anyhow), but for example, if Armin Ronacher or Robert Kern
> thumbs up a module there is a pretty good chance I will be interested
> in it.
>
> As a developer:
> * I don't want to have to maintain my code repository and my package
> releases separately.  I want to let module repository know that my
> code repository exists, and that branches tagged as "release" should
> be made available.
> * I want to maintain one README.
>
>
> I don't like "someone needs to do this now" type posts but every time
> I use PyPi it infuratiates me.  I usually end up finding modules via
> Stack Overflow, which seems silly to me.

They don't have all the features you're looking for, but at least they
seem to be working on the problem:
http://crate.io

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


Re: Naming convention for in-house modules (Newbie question)

2012-02-08 Thread Chris Rebert
On Wed, Feb 8, 2012 at 9:15 AM, HoneyMonster  wrote:
> I am quite new to Python (2.7 on Linux), and have built a few modules
> using wxPython/wxGlade for GUI elements and Psycopg2 for database access.
> I adhere mostly to the PEP8 guidelines, and use Pylint to help with
> quality control.
>
> So far I have been *very* impressed. Due to Python's straightforwardness
> and the wealth of documentation, results have been excellent.
>
> Here is my question: I would like to start an in-house library of small
> modules to import, for things like error handling/logging. That's easy
> enough, but is there a recommended way of naming such modules? I am
> concerned about avoiding name clashes with standard modules and site
> packages.

You could put all the modules under a single package; then you only
need to worry about avoiding 1 name conflict.

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


Re: MySQLdb not allowing hyphen

2012-02-08 Thread John Nagle

On 2/5/2012 2:46 PM, Chris Rebert wrote:

On Sun, Feb 5, 2012 at 2:41 PM, Emeka  wrote:


Hello All,

I noticed that MySQLdb not allowing hyphen may be way to prevent injection
attack.
I have something like below:

"insert into reviews(message, title)values('%s', '%s')" %( "We don't know
where to go","We can't wait till morrow" )

ProgrammingError(1064, "You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax to
use near 't know where to go.

How do I work around this error?


Don't use raw SQL strings in the first place. Use a proper
parameterized query, e.g.:

cursor.execute("insert into reviews(message, title) values (%s, %s)",
 ("We don't know where to go", "We can't wait till morrow"))


  Yes.  You are doing it wrong.  Do NOT use the "%" operator when
putting SQL queries together.  Let "cursor.execute" fill them
in.  It knows how to escape special characters in the input fields,
which will fix your bug and prevent SQL injection.

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


Re: Cycle around a sequence

2012-02-08 Thread Terry Reedy

On 2/8/2012 9:25 AM, Neil Cerutti wrote:

On 2012-02-08, Mark Lawrence  wrote:

I'm looking at a way of cycling around a sequence i.e. starting
at some given location in the middle of a sequence and running
to the end before coming back to the beginning and running to
the start place.  About the best I could come up with is the
following, any better ideas for some definition of better?


Python's indices were designed for these kinds of shenanigans.

def rotated(seq, n):
 """Iterate through all of seq, but starting from index n.

 >>>  ", ".join(str(n) for n in rotated(range(5), 3))
 '3, 4, 0, 1, 2'
 """

 i = n - len(seq)
 while i<  n:
 yield seq[i]
 i += 1


This is really nice, in the category of "Why didn't I think of that?"
(Probably because I knew the % mod solution from C and never 'updated'!)


if __name__ == "__main__":
 import doctest
 doctest.testmod()

If you have merely an iterable instead of a sequence, then look
to some of the other clever stuff already posted.


To make a repeating rotator is only a slight adjustment:

k = n - len(seq)
while True:
i = k
while i < n:
yield seq[i]
i += 1

--
Terry Jan Reedy

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


Komodo 7 release (Python development tools)

2012-02-08 Thread Todd Whiteman

Hello,

My name is Todd. I'm the lead developer for Komodo IDE (Interactive 
Development Environment) and Komodo Edit (a free, open-source editor) at 
ActiveState. I wanted to announce that the newest version, Komodo 7, has 
been released:


http://www.activestate.com/komodo-ide

Python has long been one of the main languages supported by Komodo, so 
we're always getting useful feedback and suggestions. For Komodo 7, 
we've incorporated a lot of this feedback into enhancing our Python 
features.


* Python Code Profiling (IDE only)
  Users have asked if there is a way to find out why their programs are 
taking so long to run. Komodo IDE 7 can show a graph of the methods and 
calls made by your program, so that you can detect where your CPU is 
being taken up.


* Sophisticated Syntax Checking
  Choose between multiple syntax checkers like PyLint, PyFlakes and 
PyChecker. Provides language-specific syntax checking for 
CSS/JavaScript/Django inside HTML template languages like Django.


* Code Collaboration (IDE only)
  We wanted to make pair programming easier. With the collaboration 
feature, multiple users can edit a document at the same time. It's kind 
of like Google Docs, but for code editing!


* Speed
  With Komodo 7 you'll notice a lot snappier Komodo start-up time, 
lower CPU utilization - particularly when idle, and lower memory usage 
for large projects.


* Even more...
  There are way more features in Komodo 7 than I can outline in a 
single post, so check out the online web pages for more Komodo 7 
enhancements:


http://www.activestate.com/komodo-ide/python-editor

Again, note that Komodo comes in two different flavours:

  1) Komodo Edit - completely free and fully open-source editor, 
offering smart code completions, syntax checking, code colorizing, 
sophisticated editing and more.


  2) Komodo IDE - a full featured IDE, offering advanced debugging, 
interactive shells, code browsing, source code control, database 
integration, unit testing, regular expression tools and more.


Try out Komodo 7 and let me know what you think. We really appreciate 
the support and feedback!


Cheers,
Todd
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing sys.path

2012-02-08 Thread John Nagle

On 2/1/2012 8:15 AM, Andrea Crotti wrote:

So suppose I want to modify the sys.path on the fly before running some
code
which imports from one of the modules added.

at run time I do
sys.path.extend(paths_to_add)

but it still doesn't work and I get an import error.


   Do

import sys

first.

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


Re: Komodo 7 release (Python development tools)

2012-02-08 Thread Terry Reedy

On 2/8/2012 3:14 PM, Todd Whiteman wrote:


My name is Todd. I'm the lead developer for Komodo IDE (Interactive
Development Environment) and Komodo Edit (a free, open-source editor) at
ActiveState. I wanted to announce that the newest version, Komodo 7, has
been released:


This is a pretty good release announcement, but a few questions.
...
>http://www.activestate.com/komodo-ide/python-editor

It would seem that the Professional Python Editor is the same as Komodo 
Edit, but it is unclear why only Python editing would be featured for 
Komodo IDE.


http://www.activestate.com/komodo-edit

is the page with the link people need to download just the editor.

Does K.Edit let me run a program with one key, like F5 in IDLE?
If so, does it leave me in interactive mode (python -i) as IDLE does?

--
Terry Jan Reedy

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


Re: Cycle around a sequence

2012-02-08 Thread Mark Lawrence

On 08/02/2012 14:25, Neil Cerutti wrote:

On 2012-02-08, Mark Lawrence  wrote:

I'm looking at a way of cycling around a sequence i.e. starting
at some given location in the middle of a sequence and running
to the end before coming back to the beginning and running to
the start place.  About the best I could come up with is the
following, any better ideas for some definition of better?


Python's indices were designed for these kinds of shenanigans.

def rotated(seq, n):
 """Iterate through all of seq, but starting from index n.

 >>>  ", ".join(str(n) for n in rotated(range(5), 3))
 '3, 4, 0, 1, 2'
 """

 i = n - len(seq)
 while i<  n:
 yield seq[i]
 i += 1

if __name__ == "__main__":
 import doctest
 doctest.testmod()

If you have merely an iterable instead of a sequence, then look
to some of the other clever stuff already posted.



The winner :)

--
Cheers.

Mark Lawrence.

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


How to make PyDev pep8 friendly?

2012-02-08 Thread Ali Zandi
Hi,

I was trying to find a way to configure PyDev e.g. in Eclipse to be
pep8 friendly.

There are a few configurations like right trim lines, use space after
commas, use space before and after operators, add new line at the end
of file which can be configured via Eclipse -> Window -> Preferences -
> PyDev -> Editor -> Code Style -> Code Formatter. But these are not
enough; for example I couldn't find a way to configure double line
spacing between function definitions.

So is there any way to configure eclipse or PyDev to apply pep8 rules
e.g. on each save?

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


frozendict

2012-02-08 Thread Evan Driscoll
Hi all,

I've been trying for a few days (only a little bit at a time) to come up
with a way of implementing a frozendict that doesn't suck. I'm gradually
converging to a solution, but I can't help but think that there's some
subtlety that I'm probably missing which is why it's not already provided.

Does anyone know why Python doesn't already come with a frozendict, or
why there seem to only be a couple attempts to write one?

Evan




signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: frozendict

2012-02-08 Thread Steven D'Aprano
On Wed, 08 Feb 2012 19:23:19 -0600, Evan Driscoll wrote:

> Hi all,
> 
> I've been trying for a few days (only a little bit at a time) to come up
> with a way of implementing a frozendict that doesn't suck. I'm gradually
> converging to a solution, but I can't help but think that there's some
> subtlety that I'm probably missing which is why it's not already
> provided.
> 
> Does anyone know why Python doesn't already come with a frozendict, or
> why there seem to only be a couple attempts to write one?

Because there is no way to implement one that doesn't suck?

Because it's a solution in search of a problem?



Actually, that's unfair. A write-once dict would be awesome for providing 
read-only constants, if only there were some way to set a namespace to 
using non-builtin dicts.



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


Re: frozendict

2012-02-08 Thread Terry Reedy

On 2/8/2012 8:23 PM, Evan Driscoll wrote:

Hi all,

I've been trying for a few days (only a little bit at a time) to come up
with a way of implementing a frozendict that doesn't suck. I'm gradually
converging to a solution, but I can't help but think that there's some
subtlety that I'm probably missing which is why it's not already provided.

Does anyone know why Python doesn't already come with a frozendict, or
why there seem to only be a couple attempts to write one?


Turn the question around: why should there be?
Python is intentionally parsimonious in adding builtins.

--
Terry Jan Reedy

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


Re: frozendict

2012-02-08 Thread Ian Kelly
On Wed, Feb 8, 2012 at 6:23 PM, Evan Driscoll  wrote:
> Hi all,
>
> I've been trying for a few days (only a little bit at a time) to come up
> with a way of implementing a frozendict that doesn't suck. I'm gradually
> converging to a solution, but I can't help but think that there's some
> subtlety that I'm probably missing which is why it's not already provided.
>
> Does anyone know why Python doesn't already come with a frozendict, or
> why there seem to only be a couple attempts to write one?

Define "doesn't suck".  If I were to hack one up, it would look
something like this:


from collections import Mapping, Hashable


class frozendict(Mapping, Hashable):

def __init__(self, *args, **kwargs):
self.__dict = dict(*args, **kwargs)

def __len__(self):
return len(self.__dict)

def __iter__(self):
return iter(self.__dict)

def __getitem__(self, key):
return self.__dict[key]

def __hash__(self):
return hash(frozenset(self.__dict.iteritems()))

def __repr__(self):
return 'frozendict(%r)' % (self.__dict,)


Not extensively tested, but it seems to work pretty well for me.

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when to use import statements in the header, when to use import statements in the blocks where they are used?

2012-02-08 Thread Steven D'Aprano
On Tue, 07 Feb 2012 21:41:49 -0500, Dave Angel wrote:

> On 02/07/2012 09:32 PM, Patto wrote:

>> However I find in the file path/to/djcelery/loaders.py from
>> django-celery source, there are so many import/from statements used
>> inside functions, I do not know why the author coded like this. Are
>> there any special facts?
>>
>>
> I can't speak for django or django-celery.  There are people that
> disagree on this, and there are some reasons to override the ones I
> mentioned.  One would be large modules that are not used in most
> circumstances, or not used till the program has run for a while.
> 
> If you put the import inside a function, you can save on startup time by
> deferring some of the logic till later.  And if there's a module that
> probably won't be used at all (eg. an error handler), perhaps you can
> avoid loading it at all.

Yes, the idea is "lazy importing" -- add the module name to the namespace, 
but don't actually load it until necessary. Apparently it is used heavily 
in Django and by PyPy.

Interestingly, the CPython developers are currently migrating the low-
level import machinery from C to pure Python (or at least mostly pure 
Python), because

(1) the C code is a mess and practically nobody understands it; 
(2) the exact import behaviour is very hard to explain; 
(3) consequently Jython, IronPython etc. may behave slightly differently;
(4) and they'd like to share the same code base as CPython; and
(5) it's really hard to write custom importers.

Moving to a pure Python implementation should fix these problems, 
provided that the speed hit isn't too big.

http://sayspy.blogspot.com.au/2012/02/how-i-bootstrapped-importlib.html


One suggestion made is that Python should support lazy imports out of the 
box.

Another reason for putting imports inside a function is that global 
imports are, well, global. If you only need access to a module from one 
function, why pollute the global namespace?

A reason for *not* importing inside a function is that there can 
sometimes be strange effects with threads, or so I've been told, but I 
couldn't begin to explain exactly what (allegedly) can go wrong.


> I still think readability trumps all the other reasons, for nearly all 
> programs.

Which is a good reason for importing inside a function: why confuse the 
reader with a global import if it isn't needed globally?



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


Re: frozendict

2012-02-08 Thread Nathan Rice
> Turn the question around: why should there be?
> Python is intentionally parsimonious in adding builtins.

For the same reason there are frozensets?

I put dicts in sets all the time.  I just tuple the items, but that
means you have to re-dict it on the way out to do anything useful with
it.  I am too lazy to write a frozendict or import one, but I would
use it if it was a builtin.


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


Id of methods

2012-02-08 Thread Emeka
Hell All,

I am trying to see if I could get more of Python without being insane.

class Boo(object):

def __init__(self , moon, sun):
self.moon = moon
self.sun = sun
def daf(self):
return self.sun + self.moon
def ball(self):
return self.sun * self.moon


print Boo.__dict__

{'__module__': '__main__', 'ball': , 'daf':
, '__dict__ ..}

print  hex(id(Boo.daf))
0x27de5a0



My question is why is it that the id of Boo.daf  is different from daf's
hex value in the above dict?

Regards, \Emeka
*Satajanus  Nig. Ltd


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


Re: Cycle around a sequence

2012-02-08 Thread Steven D'Aprano
On Wed, 08 Feb 2012 01:10:28 +, Mark Lawrence wrote:

> I'm looking at a way of cycling around a sequence i.e. starting at some
> given location in the middle of a sequence and running to the end before
> coming back to the beginning and running to the start place.

If you have a sequence, and don't mind copying it, the easiest way is 
just to slice and join:


>>> a = range(20)
>>> b = a[5:] + a[:5]
>>> print b
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]

Short, sweet, easy and simple. What's not to like about it?

For small (say, less than a few thousand of items) sequences, this 
probably is the fastest way to do it. 

Handling this lazily is trickier than it seems, because you have to store 
the first N items somewhere until you get to the rest of the iterable. 
There is no Right Way to do it, since the best solution will depend on 
how many items you have and how large N is.

Here's one way with itertools:


>>> from itertools import islice, chain, tee
>>> a = iter(range(20))
>>> t1, t2 = tee(a)
>>> b = chain(islice(t1, 5, None), islice(t2, None, 5))
>>> print list(b)
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]

But read the docs for tee first: it may be that converting to a list is 
faster and more memory efficient.

http://docs.python.org/library/itertools.html#itertools.tee


Using tee may be overkill. Here's a simpler way:

>>> a = iter(range(20))
>>> t = list(islice(a, 5))
>>> b = chain(a, t)
>>> list(b)
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]

If your data is truly humongous, already stored in a list, and you don't 
want to make a copy, then I recommend your trick of generating the 
indexes:

def cycle(seq, n):
for indexes in (xrange(n, len(seq)), xrange(n)):
for i in indexes:
yield seq[i]

If your data is humongous but only available lazily, buy more memory :)



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


standalone python web server

2012-02-08 Thread Rita
I am building a small intranet website and I would like to use Python. I
was wondering if there was a easy and medium performance python based web
server available. I would like to run it on port :8080 since I wont have
root access also I prefer something easy to deploy meaning I would like to
move the server from one physical host to another without too much fuss.

Currently, I am using flask (development server) and everything is ok but
the performance is really bad.



-- 
--- Get your facts first, then you can distort them as you please.--
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Id of methods

2012-02-08 Thread Chris Angelico
On Thu, Feb 9, 2012 at 2:48 PM, Emeka  wrote:
> I am trying to see if I could get more of Python without being insane.

Oh, I thought insanity was a prerequisite... anyway.

> print Boo.__dict__
>
> {'__module__': '__main__', 'ball': , 'daf':
> , '__dict__ ..}
>
> print  hex(id(Boo.daf))
> 0x27de5a0
>
> My question is why is it that the id of Boo.daf  is different from daf's hex
> value in the above dict?

Take no notice of the exact value of id(). It is an opaque value that
shouldn't be relied upon for anything.

That said, though, I tried your example in my two Windows Pythons,
2.6.5 and 3.2; in 3.2, the behavior is most like what you seem to be
accepting:

>>> Boo.__dict__
dict_proxy({'__module__': '__main__', 'ball': , 'daf': , '__dict__':
, '__weakref__': , '__doc__': None, '__init__':
})
>>> hex(id(Boo.daf))
'0xfcbc90'
>>> Boo.daf


Same number everywhere. But 2.6.5 has a difference that might give you a hint:

>>> print Boo.__dict__
{'__module__': '__main__', 'ball': ,
'daf': , '__dict__': , '__weakref__': , '__doc__': None, '__init__': }
>>> Boo.daf


daf is not a function, it's a special object for an unbound method.
The exact identity of it is not necessarily useful; and buried inside
it is the actual function that you defined:

>>> hex(id(Boo.daf))
'0x11b5c10'
>>> hex(id(Boo.daf.im_func))
'0x11bdc30'

And that seems to be the number that's given in the repr().

But mainly, don't rely on id() for anything beyond uniqueness against
all current objects.

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


Re: Id of methods

2012-02-08 Thread Terry Reedy

On 2/8/2012 10:48 PM, Emeka wrote:


class Boo(object):

 def __init__(self , moon, sun):
 self.moon = moon
self.sun = sun
 def daf(self):
 return self.sun + self.moon
 def ball(self):
 return self.sun * self.moon


print Boo.__dict__

{'__module__': '__main__', 'ball': , 'daf':
, '__dict__ ..}

print  hex(id(Boo.daf))
0x27de5a0



My question is why is it that the id of Boo.daf  is different from daf's
hex value in the above dict?


Because you are using Python 2. Just print Boo.daf and you will see that 
it is not a 'function'. In Python 3, it will be, and have the same address.


--
Terry Jan Reedy

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


Re: standalone python web server

2012-02-08 Thread Rodrick Brown
On Feb 8, 2012, at 11:01 PM, Rita  wrote:

> I am building a small intranet website and I would like to use Python. I was 
> wondering if there was a easy and medium performance python based web server 
> available. I would like to run it on port :8080 since I wont have root access 
> also I prefer something easy to deploy meaning I would like to move the 
> server from one physical host to another without too much fuss. 
> 
> Currently, I am using flask (development server) and everything is ok but the 
> performance is really bad.
> 

Checkout TwistedWeb it's an HTTP server that can be used as a library or 
standalone server.

$ twistd web --path . --port 8080
> 
> 
> -- 
> --- Get your facts first, then you can distort them as you please.--
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cycle around a sequence

2012-02-08 Thread Chris Angelico
On Thu, Feb 9, 2012 at 2:55 PM, Steven D'Aprano
 wrote:
> If your data is humongous but only available lazily, buy more memory :)

Or if you have a huge iterable and only need a small index into it,
snag those first few entries into a list, then yield everything else,
then yield the saved ones:

def cycle(seq,n):
seq=iter(seq)
lst=[next(seq) for i in range(n)]
try:
while True: yield next(seq)
except StopIteration:
for i in lst: yield i

>>> list(cycle(range(10,20),2))
[12, 13, 14, 15, 16, 17, 18, 19, 10, 11]

Requires storage space relative to n, regardless of the length of the iterator.

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


Re: standalone python web server

2012-02-08 Thread Roy Smith
In article ,
 Rodrick Brown  wrote:

> On Feb 8, 2012, at 11:01 PM, Rita  wrote:
> 
> > I am building a small intranet website and I would like to use Python. I 
> > was wondering if there was a easy and medium performance python based web 
> > server available. I would like to run it on port :8080 since I wont have 
> > root access also I prefer something easy to deploy meaning I would like to 
> > move the server from one physical host to another without too much fuss. 
> > 
> > Currently, I am using flask (development server) and everything is ok but 
> > the performance is really bad.
> > 
> 
> Checkout TwistedWeb it's an HTTP server that can be used as a library or 
> standalone server.
> 
> $ twistd web --path . --port 8080
>

Another one to look at is gunicorn.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Komodo 7 release (Python development tools)

2012-02-08 Thread anon hung
>> My name is Todd. I'm the lead developer for Komodo IDE (Interactive
>> Development Environment) and Komodo Edit (a free, open-source editor) at
>> ActiveState. I wanted to announce that the newest version, Komodo 7, has
>> been released:
>
> This is a pretty good release announcement, but a few questions.
> ...
>  >http://www.activestate.com/komodo-ide/python-editor
>
> It would seem that the Professional Python Editor is the same as Komodo
> Edit, but it is unclear why only Python editing would be featured for
> Komodo IDE.
>
> http://www.activestate.com/komodo-edit
>
> is the page with the link people need to download just the editor.
>
> Does K.Edit let me run a program with one key, like F5 in IDLE?

AFAIK it sure does!

> If so, does it leave me in interactive mode (python -i) as IDLE does?

Hmmm, I don't know about that.

Best,
anonhung


-- 
http://spreadingviktororban.weebly.com";>Viktor Orban Prime
minister of Hungary
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turbogears 1

2012-02-08 Thread anon hung
>> Hey guys, someone asked me to maintain his old website, trouble is,
>> it's in python, more trouble is it's in turbogears 1. I'm not fluent
>> in python but all right, I can learn, but this turbogears
>> thing..
>>
>> First of all, is it still alive? Looks like turbogears 2 is the most
>> recent version but even that is being abandoned.
>
> Yup, looks dead to me.  Hasn't had a release in almost 2 months or a
> commit to the GIT repo in 2 days.  Must be nailed to its perch or
> something.
>
> http://turbogears.org/en/current-status
> http://sourceforge.net/p/turbogears2/tg2/commit_browser

All right, what I got confused about is that they talk about pyramid
these days which will not be turbogears 2 based.

>> Am I basically given vaporware? Does anyone have any up to date info?
>
> Have you considered Ruby on Rails?

This is joke, right? :)

Best,
anonhung


-- 
Viktor Orban Prime minister of Hungary
http://spreadingviktororban.weebly.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turbogears 1

2012-02-08 Thread anon hung
>>> Hey guys, someone asked me to maintain his old website, trouble is,
>>> it's in python, more trouble is it's in turbogears 1. I'm not fluent in
>>> python but all right, I can learn, but this turbogears thing..
>>>
>>> First of all, is it still alive? Looks like turbogears 2 is the most
>>> recent version but even that is being abandoned.
>>
>> Yup, looks dead to me.  Hasn't had a release in almost 2 months or a
>> commit to the GIT repo in 2 days.  Must be nailed to its perch or
>> something.
>>
>> http://turbogears.org/en/current-status
>> http://sourceforge.net/p/turbogears2/tg2/commit_browser
>
> Ohhh, sarcasm...
>
> To the original poster: what makes you think that Turbogears 2 is
> abandoned?

As I've said in another reply they keep talking about pyramid as if
that will be a totally new framework. I'm not saying I know things
definitely but surely my impression was that the team is out for
something new not based on turbogears 2.

>>> Am I basically given vaporware? Does anyone have any up to date info?
>>
>> Have you considered Ruby on Rails?
>
> Now that's just cruel.

Yep, it is :)

Best,
anonhung

-- 
Viktor Orban Prime minister of Hungary
http://spreadingviktororban.weebly.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: turbogears 1

2012-02-08 Thread anon hung
>>> Hey guys, someone asked me to maintain his old website, trouble is,
>>> it's in python, more trouble is it's in turbogears 1. I'm not fluent in
>>> python but all right, I can learn, but this turbogears thing..
>>>
>>> First of all, is it still alive? Looks like turbogears 2 is the most
>>> recent version but even that is being abandoned.
>>
>> Yup, looks dead to me.  Hasn't had a release in almost 2 months or a
>> commit to the GIT repo in 2 days.  Must be nailed to its perch or
>> something.
>>
>> http://turbogears.org/en/current-status
>> http://sourceforge.net/p/turbogears2/tg2/commit_browser
>
> Ohhh, sarcasm...
>
> To the original poster: what makes you think that Turbogears 2 is
> abandoned?

The development team keeps talking about pyramid which is supposed to
be a totally new framework. I don't pretend to understand the work
flow but surely they got me confused about switching gears (no pun
intended!).

>>> Am I basically given vaporware? Does anyone have any up to date info?
>>
>> Have you considered Ruby on Rails?
>
> Now that's just cruel.

Yep, it is :)

Best,
anonhung


-- 
Viktor Orban Prime minister of Hungary
http://spreadingviktororban.weebly.com



-- 
Viktor Orban Prime minister of Hungary
http://spreadingviktororban.weebly.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: standalone python web server

2012-02-08 Thread anon hung
>> I am building a small intranet website and I would like to use Python. I
>> was wondering if there was a easy and medium performance python based web
>> server available. I would like to run it on port :8080 since I wont have
>> root access also I prefer something easy to deploy meaning I would like
>> to
>> move the server from one physical host to another without too much fuss.
>>
>> Currently, I am using flask (development server) and everything is ok but
>> the performance is really bad.

How about cherrypy?

Best,
anonhung


-- 
Viktor Orban Prime minister of Hungary
http://spreadingviktororban.weebly.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: frozendict

2012-02-08 Thread Evan Driscoll
On 13:59, Nathan Rice wrote:
>> Turn the question around: why should there be?
>> Python is intentionally parsimonious in adding builtins.
> 
> For the same reason there are frozensets?
> 
> I put dicts in sets all the time.  I just tuple the items, but that
> means you have to re-dict it on the way out to do anything useful with
> it.  I am too lazy to write a frozendict or import one, but I would
> use it if it was a builtin.

I've wanted to do that as well.

My current use case is I want to have a dict as an attribute of another
object, and I want to use that object as a key in a dictionary. That
means that the outer object has to be immutable (an obnoxious enough
task on its own, BTW) and that either the dict itself has to be excluded
from computing the hash or the dict also has to be immutable.


Also, it's not like it has to be a builtin, per se. I know how to spell
'import'. :-)

Evan



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: frozendict

2012-02-08 Thread Evan Driscoll
On 13:59, Ian Kelly wrote:
>
> Define "doesn't suck".  If I were to hack one up, it would look
> something like this:
>
>
> from collections import Mapping, Hashable

So part of my objection was that I wanted to make sure I got all of the
expected functionality, and that takes a bunch of functions. I didn't
know about the ABTs in 'collections' though, so that helps a bit.

However, I'd still prefer something that guaranteed immutability better
than that. I might just be fighting the language too much at that point
though...

Evan




signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Can not get the result of query in pymssql module of python...

2012-02-08 Thread amarjeet yadav
Hi All,
   This is my first post to any mailing group. I am QA engg and
using python for testing automation. I need to connect with Mysql
(2008) and mssql databases for executing some queries.

I have installed following modules & softwares in python 2.7 :

python 2.7.2
setuptools
MySQLdb Module
pymssql module
yum install mysql msql-devel freetdf

I have installed freetds 0.9version. After installation of all the above
components, I have done following things

Python 2.6 (r26:66714, Apr  8 2010, 08:46:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> conn = pymssql.connect(host='mssql_server', user='_username',
password='_password', database='_db',as_dict=True)
>>> cur = conn.cursor()
>>> cur.execute('select count(*) from D2.dbo.abc (nolock)')
>>> print cur.fetchall()
[]
>>> cur.rowcount
-1
>>> exit()

I am expecting that the result of the query will be 16. But it is not
retuning any data from query with no error at any place. On execting the
same query in tsql, I got result as 16.

freetds.log file  :

mem.c:615:tds_free_all_results()
util.c:156:Changed query state from IDLE to QUERYING
write.c:140:tds_put_string converting 50 bytes of "select count(*) from
D2.dbo.abc (nolock)"
write.c:168:tds_put_string wrote 100 bytes
util.c:156:Changed query state from QUERYING to PENDING
net.c:741:Sending packet
 01 01 00 6c 00 00 01 00-73 00 65 00 6c 00 65 00 |...l s.e.l.e.|
0010 63 00 74 00 20 00 63 00-6f 00 75 00 6e 00 74 00 |c.t. .c. o.u.n.t.|
0020 28 00 2a 00 29 00 20 00-66 00 72 00 6f 00 6d 00 |(.*.). . f.r.o.m.|
0030 20 00 44 00 32 00 2e 00-64 00 62 00 6f 00 2e 00 | .D.2... d.b.o...|
0040 61 00 63 00 74 00 69 00-76 00 65 00 5f 00 61 00 |a.c.t.i. v.e._.a.|
0050 67 00 65 00 6e 00 74 00-73 00 20 00 28 00 6e 00 |g.e.n.t. s. .(.n.|
0060 6f 00 6c 00 6f 00 63 00-6b 00 29 00 |o.l.o.c. k.).|
dblib.c:4639:dbsqlok(0x99d1148)
dblib.c:4669:dbsqlok() not done, calling tds_process_tokens()
token.c:540:tds_process_tokens(0x998ff70, 0xbff42098, 0xbff42094, 0x6914)
util.c:156:Changed query state from PENDING to READING
net.c:555:Received header
 04 01 00 21 00 46 01 00-|...!.F..|
net.c:609:Received packet
 04 01 00 21 00 46 01 00-81 01 00 00 00 01 00 26 |...!.F.. ...&|
0010 04 00 d1 04 10 00 00 00-fd 10 00 c1 00 01 00 00 | |
0020 00 -|.|
token.c:555:processing result tokens.  marker is  81(TDS7_RESULT)
token.c:1515:processing TDS7 result metadata.
mem.c:615:tds_free_all_results()
token.c:1540:set current_results (1 column) to tds->res_info
token.c:1547:setting up 1 columns
token.c:1486:tds7_get_data_info:
colname =  (0 bytes)
type = 38 (integer-null)
server's type = 38 (integer-null)
column_varint_size = 1
column_size = 4 (4 on server)
token.c:1556: name size/wsize  type/wtype  utype
token.c:1557:  --- --- ---
token.c:1567:4/438/380
util.c:156:Changed query state from READING to PENDING
dblib.c:4700:dbsqlok() found result token
dblib.c:1813:dbnumcols(0x99d1148)
dblib.c:2761:dbcount(0x99d1148)
dblib.c:1813:dbnumcols(0x99d1148)
dblib.c:1839:dbcolname(0x99d1148, 1)
dblib.c:2831:dbcoltype(0x99d1148, 1)
dblib.c:2018:dbnextrow(0x99d1148)
dblib.c:2031:dbnextrow() dbresults_state = 1 (_DB_RES_RESULTSET_EMPTY)
dblib.c:2036:leaving dbnextrow() returning -2 (NO_MORE_ROWS)
dblib.c:2761:dbcount(0x99d1148)
dblib.c:1443:dbclose(0x99d1148)
dblib.c:258:dblib_del_connection(0xeff460, 0x998ff70)
mem.c:615:tds_free_all_results()
util.c:156:Changed query state from PENDING to DEAD
dblib.c:305:dblib_release_tds_ctx(1)
dblib.c:5882:dbfreebuf(0x99d1148)




Please let me know what could be the issue? Am I forgettting to set any
variable ot conf file change. Is anyone has faced the same problem.


Thanks in Advance for your help
-- 
http://mail.python.org/mailman/listinfo/python-list