Re: Python and TAP

2012-02-07 Thread Matej Cepl

On 7.2.2012 04:24, alex23 wrote:

Experience?

Are you seriously advocating something for which you've done nothing
more than watch a podcast?


No, I am not. If you reread my original post, you may find that I was 
asking exactly for experience and explanation why something which seems 
to me obvious is not done. I guess there must be some hook somewhere, 
right? Which is what I was asking for.


One hook I've got (YAMLish is really ... well, let's keep this group G 
rated), others may yet to follow.


Matěj
--
http://mail.python.org/mailman/listinfo/python-list


python file synchronization

2012-02-07 Thread silentnights
Hi All,

I have the following problem, I have an appliance (A) which generates
records and write them into file (X), the appliance is accessible
throw ftp from a server (B). I have another central server (C) that
runs a Django App, that I need to get continuously the records from
file (A).

The problems are as follows:
1. (A) is heavily writing to the file, so copying the file will result
of uncompleted line at the end.
2. I have many (A)s and (B)s  that I need to get the data from.
3. I can't afford losing any records from file (X)

My current implementation is as follows:
1. Server (B) copy the file (X) throw FTP.
2. Server (B) make a copy of file (X) to file (Y.time_stamp) ignoring
the last line to avoid incomplete lines.
3. Server (B) periodically make copies of file (X) and copy the lines
starting from previous ignored line to file (Y.time_stamp)

4. Server (C) mounts the diffs_dir locally.
5. Server (C) create file (Y.time_stamp.lock) on target_dir then copy
file (Y.time_stamp) to local target_dir then delete
(Y.time_stamp.lock)

6. A deamon running in Server (C) read file list from the target_dir,
and process those file that doesn't have a matching *.lock file, this
procedure to avoid reading the file until It's completely copied.

The above is implemented and working, the problem is that It required
so many syncs and has a high overhead and It's hard to debug.

I greatly appreciate your thoughts and suggestions.

Lastly I want to note that am not a programming guru, still a noob,
but I am trying to learn from the experts. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between random module in python 2.6 and 3.2?

2012-02-07 Thread Serhiy Storchaka
07.02.12 00:06, Matej Cepl написав(ла):
> return seq[int(random.random() * len(seq))]
> 
> doesn't seem like something so terrible (and maintenance intense). :)

_choice('abc') returns 'a' with probability P('a') = 
1501199875790165/4503599627370496 = 1/3 - 1/13510798882111488 and 'b' with 
probability P('b') = 3002399751580331/9007199254740992 = 1/3 + 
1/27021597764222976. P('b') - P('a') = 1/9007199254740992.

This may be acceptable for your application, but for applications that are 
concerned not only about the repeatability of results, but the accuracy and 
consistency with the results obtained by other (not Python) applications, it is 
better to get rid of such a notorious bias.

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


Re: PythonWin debugger holds onto global logging objects too long

2012-02-07 Thread Jean-Michel Pichavant

Vinay Sajip wrote:

On Jan 24, 2:52 pm, Rob Richardson  wrote:
  

I use PythonWin to debug the Python scripts we write.  Our scripts often use 
the log2pyloggingpackage.  When running the scripts inside the debugger, we 
seem to get oneloggingobject for every time we run the script.  The result is 
that after running the script five times, the log file contains five copies of 
every message.  The only way I know to clean this up and get only a single copy 
of each message is to close PythonWin and restart it.

What do I have to do in my scripts to clean up theloggingobjects so that I 
never get more than one copy of each message in my log files?




I don't know what log2py is - Google didn't show up anything that
looked relevant. If you're talking about the logging package in the
Python standard library, I may be able to help: but a simple script
that I ran in PythonWin didn't show any problems, so you'll probably
need to post a short script which demonstrates the problem when run in
PythonWin.

Regards,

Vinay Sajip
  

Same here, can't find anything about log2py.
Anyway it's possible that your pythonwin does not spawn a clean python 
interpreter for every run, keeping the same one.


So you could possibly keep adding log handlers to your loggers because 
they may be static objects (like for the standard logging module).

One solution would be to empty your logger handler list before adding any.

I'm just guessing though, difficult to know without any info on log2py.

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


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Jean Dupont
On 7 feb, 06:07, Roy Smith  wrote:
> In article
> ,
>  Jean Dupont  wrote:
>
> > I'd like to read in a stream of data which looks like this:
> > the device sends out a byte-string of 11 bytes roughly every second:
>
> >     B0B0B0B0B03131B0B50D8A
> >     B0B0B0B0B03131B0B50D8A
> >     B0B0B031B63131B0310D8A
> >     B0B034B3323432B3310D8A
> >     B0B03237B53432B3310D8A
> > .
> > .
> > .
>
> > As you see every string is ended by 0D8A
> > How can this be accomplished in Python?
>
> The basic idea would be to open your datastream in binary mode
> (http://docs.python.org/library/functions.html#open), then use read(11)
> to read exactly 11 bytes into a string.
>
> Depending on what the 11 bytes are, you might want to use the struct
> module (http://docs.python.org/library/struct.html) to extract the data
> in a more useful form.

Thank you very much for taking the time to reply. I'm really
completely new to python and all help is really very welcome.
In the documentation I read that to open the datastream binary I need
to add the option b
this is how far I got until now:
#!/usr/bin/python
import serial, time, os
voltport='/dev/ttyUSB2'
print "Enter a filename:",
filename = raw_input()
voltdata = open(filename,'wb')
ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)
ser2.setDTR(level=True)
print "State of DSR-line: ", ser2.getDSR()
#the following line was added because I want to be sure that all
parameters are set the same as under a working application for the
same device
os.system("stty -F31:0:bbb:
0:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0")
print "Opening " + ser2.portstr
s =ser2.read(11) #read up to 11bytes
voltdata.write(s)
ser2.close()
voltdata.close()

However the above code doesn't fill my file with data, I guess the
data should also be flushed somewhere in the code but I'm unsure where
to do that.
A futher consideration: because the device sends its data continuously
I guess I'd have to use the byte sequence 0D8A of the previously sent
data string as an indicator that the next 9 bytes are those I really
want and put those in a string which than coudl be written to the file

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


Static HTML documentation from docstrings

2012-02-07 Thread Florian Weimer
I'm slightly confused about docstrings and HTML documentation.  I used
to think that the library reference was (in part) generated from the
source code, but this does not seem to be the case.

Is there any tool support for keeping documentation and code in sync?
-- 
http://mail.python.org/mailman/listinfo/python-list


iterating over list with one mising value

2012-02-07 Thread Sammy Danso
Hello experts,
I am having trouble accessing the content of my list. 
my list content has 2-pair value with the exception of one which has single 
value. here is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
 
I am unable to iterate through list to access invidual value pairs
 
I get an error message saying ' the list should more than 1 value pairs'. I 
guess because 'd' has no value. How do I solve this problem?
 
Your help would be much appreciated.
 
Thanks
Sammy
___
http://www.comp.leeds.ac.uk/scsod/-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static HTML documentation from docstrings

2012-02-07 Thread Chris Rebert
On Tue, Feb 7, 2012 at 4:11 AM, Florian Weimer  wrote:
> I'm slightly confused about docstrings and HTML documentation.  I used
> to think that the library reference was (in part) generated from the
> source code, but this does not seem to be the case.
>
> Is there any tool support for keeping documentation and code in sync?

Yes. Sphinx's "autodoc" extension allows the placement of API
documentation directly in docstrings and comments:
http://sphinx.pocoo.org/ext/autodoc.html

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


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Antti J Ylikoski

On 7.2.2012 14:13, Jean Dupont wrote:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)


In Python, if you want to continue the source line into the next text 
line, you must end the line to be continued with a backslash '\'.


So you should write:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \
rtscts=0, dsrdtr=0, timeout=15)

and analogously.

Hope that this will help.  Andy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Peter Otten
Antti J Ylikoski wrote:

> On 7.2.2012 14:13, Jean Dupont wrote:
>> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>> rtscts=0, dsrdtr=0, timeout=15)
> 
> In Python, if you want to continue the source line into the next text
> line, you must end the line to be continued with a backslash '\'.
> 
> So you should write:
> 
> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \
> rtscts=0, dsrdtr=0, timeout=15)
> 
> and analogously.
> 
> Hope that this will help.  Andy.

This is wrong. A line with an open parenthesis is continued automatically:

>>> zip("abc",
... "def")
[('a', 'd'), ('b', 'e'), ('c', 'f')]
>>> ("abc"
... "def")
'abcdef'
>>> 1 + 2 + (
... 3)
6


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


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Heiko Wundram

Am 07.02.2012 14:48, schrieb Antti J Ylikoski:

On 7.2.2012 14:13, Jean Dupont wrote:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)


In Python, if you want to continue the source line into the next text
line, you must end the line to be continued with a backslash '\'.


Absolutely not true, and this is bad advice (stylistically).

When (any form of) brackets are open at the end of a line, Python does 
not start a new command on the next line but rather continues the 
backeted content.


So:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
 rtscts=0, dsrdtr=0, timeout=15)

is perfectly fine and certainly the recommended way of putting this.

Adding the backslash-continuation is always _possible_, but only 
_required_ when there are no open brackets.


So:

x = "hello" \
" test"

is equivalent to:

x = ("hello"
 " test")

in assigning:

x = "hello test"

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


Re: iterating over list with one mising value

2012-02-07 Thread Dave Angel

On 02/07/2012 07:27 AM, Sammy Danso wrote:

Hello experts,
I am having trouble accessing the content of my list.
my list content has 2-pair value with the exception of one which has single 
value. here is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
  
I am unable to iterate through list to access invidual value pairs
  
I get an error message saying ' the list should more than 1 value pairs'. I guess because 'd' has no value. How do I solve this problem?
  
Your help would be much appreciated.
  
Thanks

Sammy
The real answer is to figure out how it gets into that state.  Is the 
input file invalid?  Or you have a device that sometimes skips one?


But if you're stuck with the data in that list:

If the list is of odd size, truncate it.  Then your loop should not run 
into an uneven pair.


Of course if you actually posted the code, or even the real and complete 
error message, we might be able to make other suggestions.




--

DaveA

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


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Jean Dupont
On 7 feb, 15:04, Heiko Wundram  wrote:
> Am 07.02.2012 14:48, schrieb Antti J Ylikoski:
>
> > On 7.2.2012 14:13, Jean Dupont wrote:
> >> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
> >> rtscts=0, dsrdtr=0, timeout=15)
>
> > In Python, if you want to continue the source line into the next text
> > line, you must end the line to be continued with a backslash '\'.
>
> Absolutely not true, and this is bad advice (stylistically).
>
> When (any form of) brackets are open at the end of a line, Python does
> not start a new command on the next line but rather continues the
> backeted content.
>
> So:
>
> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>                       rtscts=0, dsrdtr=0, timeout=15)
>
> is perfectly fine and certainly the recommended way of putting this.
>
> Adding the backslash-continuation is always _possible_, but only
> _required_ when there are no open brackets.
>
> So:
>
> x = "hello" \
>      " test"
>
> is equivalent to:
>
> x = ("hello"
>       " test")
>
> in assigning:
>
> x = "hello test"
>
> --
> --- Heiko.

Hello to all who gave advice concerning the line continuation, in fact
this was not a real problem but happened by accident
copying and pasting my program lines. Advice concerning the empty file
would of course also be very much appreciated.

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


smart baba new year special offer

2012-02-07 Thread Smart Baba
Smart Baba special limited offer. We are the only world lowest-cost,
yet professional and elegant-designing website developing company.
Follow us for further details:
www.websitedeals.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between random module in python 2.6 and 3.2?

2012-02-07 Thread Ulrich Eckhardt

Am 06.02.2012 09:45, schrieb Matej Cepl:

Also, how could I write a re-implementation of random.choice which would
work same on python 2.6 and python 3.2? It is not only matter of unit
tests, but I would really welcome if the results on both versions
produce the same results.


Two approaches come to mind:
1. make two runs and verify that they differ
This is a bit problematic, because there is a small but nonzero chance 
that two runs don't differ. Random numbers are just not random enough. 
Anyhow, afterwards, sort the results again and verify that it was just 
the order that differs.


2. hack the random module into something nonrandom
I think you can "import debug_random as random" and then have your 
testee automatically pick up that module instead of the real one. Since 
you already hardcoded the results to check against ("expected = ..." in 
your code), you can also hard-code the sequence of random numbers 
corresponding to that. This is even cleaner, as you don't rely on 
something being deterministic that is supposed to be random, which is 
not totally surprising but still somehow paradox.


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


Re: iterating over list with one mising value

2012-02-07 Thread Rick Johnson
On Feb 7, 8:46 am, Dave Angel  wrote:
> On 02/07/2012 07:27 AM, Sammy Danso wrote:> Hello experts,
> > I am having trouble accessing the content of my list.
> > my list content has 2-pair value with the exception of one which has single 
> > value. here is an example  ['a', 1, 'b', 1, 'c', 3, 'd']

First you need to explain why you're using a flat list when a number
of other structures would be the better choice; like a list of tuples
or a dict.

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


Re: iterating over list with one mising value

2012-02-07 Thread Ian Kelly
On Tue, Feb 7, 2012 at 5:27 AM, Sammy Danso  wrote:
>
> Hello experts,
> I am having trouble accessing the content of my list.
> my list content has 2-pair value with the exception of one which has single 
> value. here is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
>
> I am unable to iterate through list to access invidual value pairs
>
> I get an error message saying ' the list should more than 1 value pairs'. I 
> guess because 'd' has no value. How do I solve this problem?

What are you using to iterate through pairs instead of individual
elements?  It sounds like it is not very flexible.  I would recommend
instead using the "grouper" recipe from the itertools documentation:

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

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


Re: iterating over list with one mising value

2012-02-07 Thread Tim Chase

On 02/07/12 09:34, Rick Johnson wrote:

On Feb 7, 8:46 am, Dave Angel  wrote:

On 02/07/2012 07:27 AM, Sammy Danso wrote:>  Hello experts,

I am having trouble accessing the content of my list. my
list content has 2-pair value with the exception of one
which has single value. here is an example  ['a', 1, 'b',
1, 'c', 3, 'd']


First you need to explain why you're using a flat list when a
number of other structures would be the better choice; like a
list of tuples or a dict.


I'm not sure there's really a need for the OP to explain 
why...perhaps the data comes from an untrusted (or incompetent) 
source and the routine the OP is asking about is to *create* such 
a list-of-tuples or a dict.


The more important question is what the user/app wants to do when 
such a case is encountered:  do they want to ignore the unpaired 
value, or do they want to pad it with a default value?


-tkc



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


Re: how to read serial stream of data [newbie]

2012-02-07 Thread Antti J Ylikoski

On 7.2.2012 16:02, Peter Otten wrote:

Antti J Ylikoski wrote:


On 7.2.2012 14:13, Jean Dupont wrote:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)


In Python, if you want to continue the source line into the next text
line, you must end the line to be continued with a backslash '\'.

So you should write:

ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \
rtscts=0, dsrdtr=0, timeout=15)

and analogously.

Hope that this will help.  Andy.


This is wrong. A line with an open parenthesis is continued automatically:


zip("abc",

... "def")
[('a', 'd'), ('b', 'e'), ('c', 'f')]

("abc"

... "def")
'abcdef'

1 + 2 + (

... 3)
6




Thank you for correcting me.  Andy.

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


Reading files in from the proper directory

2012-02-07 Thread SMac2347
Hello. I am admittedly a Python novice, and ran into some trouble
trying to write a program that will pull multiple excel files all into
one file, with each file on a different sheet.

I am confident most of the code is correct, as the program runs
without any errors and I found the base of it online, making changes
as necessary for my own purposes. However, I am having trouble
specifying the exact directory where my code should be pulling the
files from. All the files are in the same folder, and I have put the
folder on my desktop. Am I correct in thinking that I need to change
the current working directory to this folder in order for Python to
read in these files, then generate my output? Or should I be doing
something else?

Any and all help is appreciated, thanks!

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


Re: Reading files in from the proper directory

2012-02-07 Thread Dave Angel

On 02/07/2012 01:14 PM, smac2...@comcast.net wrote:

Hello. I am admittedly a Python novice, and ran into some trouble
trying to write a program that will pull multiple excel files all into
one file, with each file on a different sheet.

I am confident most of the code is correct, as the program runs
without any errors and I found the base of it online, making changes
as necessary for my own purposes. However, I am having trouble
specifying the exact directory where my code should be pulling the
files from. All the files are in the same folder, and I have put the
folder on my desktop. Am I correct in thinking that I need to change
the current working directory to this folder in order for Python to
read in these files,
No, Python certainly does not constrain you to working with files only 
in the current working directory.  My rule of thumb is never to change 
the cwd in a Python program.  You can use relative paths to open files, 
or you can use absolute paths.  There is even a library function 
os.path.abspath() for converting a relative path to an absolute one.


If you do change cwd during the running of a program, then relative 
filenames that worked earlier might no longer work.  You could convert 
them all to absolute paths, but that's more work.


You can piece together path strings using os.path.join().  It's smart 
enough to know the path separator for your particular platform.


Check out this page: http://docs.python.org/library/os.path.html


then generate my output? Or should I be doing
something else?

Any and all help is appreciated, thanks!




--

DaveA

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


keeping twisted and wxPython in sync

2012-02-07 Thread Littlefield, Tyler

Hello all:
I have a couple questions. First, is there a way to know if connectTCP 
failed? I am writing a client with Twisted and would like to be able to 
notify the user if they couldn't connect.
Second, I set the protocol on my factory after a connection has been 
made. So when I send my user and password, that is when I connect. Is 
there a way to handle waiting for the connection to complete?


--

Take care,
Ty
Web: http://tds-solutions.net
The Aspen project: a light-weight barebones mud engine
http://code.google.com/p/aspenmud

Sent from my toaster.

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


Re: Reading files in from the proper directory

2012-02-07 Thread Peter Otten
smac2...@comcast.net wrote:

> Hello. I am admittedly a Python novice, and ran into some trouble
> trying to write a program that will pull multiple excel files all into
> one file, with each file on a different sheet.
> 
> I am confident most of the code is correct, as the program runs
> without any errors and I found the base of it online, making changes
> as necessary for my own purposes. 

That confidence usually evaporates once you write the first unit test ;)

> However, I am having trouble
> specifying the exact directory where my code should be pulling the
> files from. All the files are in the same folder, and I have put the
> folder on my desktop. Am I correct in thinking that I need to change
> the current working directory to this folder in order for Python to
> read in these files, then generate my output? Or should I be doing
> something else?

Do it properly, allow specifying the files on the commandline:

import argparse

def process_files(files, destfile):
# put your real code here
print "merge " + "\n  ".join(files)
print "into  " + destfile

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("files", metavar="file", nargs="+")
parser.add_argument("destfile")
args = parser.parse_args()
process_files(args.files, args.destfile)

If you have standard locations for sources and destination you can wrap your 
python script into a little batch file containing something like

python \source\path\*.xls \dest\path\merged.xls

and invoke that to get both flexibility and convenience.

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


Re: Reading files in from the proper directory

2012-02-07 Thread SMac2347
Thanks for the responses.


Below is the code I have thus far. while the program runs glitch-free,
it only results in the printing of the message: "NOTE *** No xls files
in C:/Documents and Settings/smacdon/." as specified by my code. Any
idea as to why it might be unable to find the .xls documents (yes they
are .xls documents and not .xlsx). Thanks!


import xlrd, xlwt
import glob, os.path
def merge_xls (in_dir, out_file="C:\Documents and Settings\smacdon
\Desktop\09 Aggregate JWS\09_merged_data.xls"):
xls_files   = glob.glob(in_dir + "*.xls")
sheet_names = [os.path.basename(v)[:-4] for v in xls_files]
sheet_excl  = [os.path.basename(v)[:-4] for v in xls_files if len
(os.path.basename(v)[:-4]) > 29]
merged_book = xlwt.Workbook()
if in_dir[-1:] != "/": in_dir = in_dir + "/"
xls_files.sort()
if xls_files:
for k, xls_file in enumerate(xls_files):
print "---> Processing file %s" % (xls_file)
if len (sheet_names[k]) <= 29:
book = xlrd.open_workbook(xls_file)
if book.nsheets == 1:
ws  = merged_book.add_sheet(sheet_names[k])
sheet = book.sheet_by_index(0)
for rx in range(sheet.nrows):
for cx in range(sheet.ncols):
ws.write(rx, cx, sheet.cell_value(rx,
cx))
elif book.nsheets in range(2, 100):
for sheetx in range(book.nsheets):
sheet0n = sheet_names[k]+str(sheetx
+1).zfill(2)
ws = merged_book.add_sheet(sheet0n)
sheet = book.sheet_by_index(sheetx)
for rx in range(sheet.nrows):
for cx in range(sheet.ncols):
ws.write(rx, cx, sheet.cell_value(rx,
cx))
else:
print "ERROR *** File %s has %s sheets (maximum is
99)" % (xls_file, book.nsheets)
raise
else:
print "WARNING *** File name too long: <%s.xls>
(maximum is 29 chars) " % (sheet_names[k])
print "WARNING *** File <%s.xls> was skipped." %
(sheet_names[k])
merged_book.save(out_file)
print
print "---> Merged xls file written to %s using the following
source files: " % (out_file)
for k, v in enumerate(sheet_names):
if len(v) <= 29:
print "\t", str(k+1).zfill(3), "%s.xls" % (v)
print
if sheet_excl:
print "--> The following files were skipped because the
file name exceeds 29 characters: "
for k, v in enumerate(sheet_excl):
print "\t", str(k+1).zfill(3), v
else:
print "NOTE *** No xls files in %s." % (in_dir)
merge_xls(in_dir="C:\Documents and Settings\smacdon\Desktop\09
Aggregate JWS"
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-02-07 Thread Jean Dupont
On 7 feb, 05:21, Terry Reedy  wrote:
> On 2/2/2012 3:57 PM, Jean Dupont wrote:
>
> > I'd like to read in the output of a voltcraft vc960 voltmeter
> > connected to a usb-port.
> > I found the perl-script below but I'd like to accomplish the same with
> > python:
>
> The script below is for an old-fashioned, slow, multiple-pin serial
> port, not usb. I don't know anything about interfacing through usb.
> Recheck what the voltmeter actually connects to.
The voltmeter uses an optical rs232-connection, that is "good enough
technology" for this purpose. But as I don't have a computer with real
rs232 ports I use a rs232toUSB adapter which presents itself to the
linux-computer as /dev/ttyUSBx.
>
> > I guess I have to use the module serial but I don't know how I should
> > set the serial parameters so they are the same as in the perl-script.
> > Could someone supply me the command for setting the serial-parameters
> > correctly in Python?
>
> Last I know, pyserial is also for old serial ports. Setting the
> properties should be pretty obvious from the manual or code.
>
It is not so obvious as you might think, one reason being the
handshake line(s?) are used in an unconvential way to supply power to
the rs232-optical interface
> There are also python usb 
> modules.http://sourceforge.net/projects/mysql-python/?source=directory
I followed this link but all I found was something concerning
mysql...???
>
anyway, thanks for trying to help

Jean
>
>
>
>
>
>
> > #!/usr/bin/perl
>
> > use strict;
> > use warnings;
>
> > use Device::SerialPort;
>
> > die("Usage: $0 /dev/ttyS0\n") unless $#ARGV == 0;
>
> > my ($devicepath) = @ARGV;
>
> > my $port = new Device::SerialPort($devicepath);
> > die "Couldn't open serial port" if ! defined $port;
>
> > $port->baudrate(2400);
> > $port->databits(8);
> > $port->parity("none");
> > $port->stopbits(1);
> > $port->handshake("none");
> > $port->rts_active(0);
> > $port->dtr_active(1);
>
> > #$port->read_char_time(5);     # wait 5ms per character
> > $port->read_const_time(200);   # 0.2 second per unfulfilled "read"
> > call
> > $| = 1; # autoflush STDOUT
> > while(1) {
> >          my ($nin, $in) = $port->read(255);
> >          print $in;
> > }
>
> > $port->close;
>
> --
> Terry Jan Reedy

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


Re: Reading files in from the proper directory

2012-02-07 Thread SMac2347
On Feb 7, 1:40 pm, Dave Angel  wrote:
> On 02/07/2012 01:14 PM, smac2...@comcast.net wrote:> Hello. I am admittedly a 
> Python novice, and ran into some trouble
> > trying to write a program that will pull multiple excel files all into
> > one file, with each file on a different sheet.
>
> > I am confident most of the code is correct, as the program runs
> > without any errors and I found the base of it online, making changes
> > as necessary for my own purposes. However, I am having trouble
> > specifying the exact directory where my code should be pulling the
> > files from. All the files are in the same folder, and I have put the
> > folder on my desktop. Am I correct in thinking that I need to change
> > the current working directory to this folder in order for Python to
> > read in these files,
>
> No, Python certainly does not constrain you to working with files only
> in the current working directory.  My rule of thumb is never to change
> the cwd in a Python program.  You can use relative paths to open files,
> or you can use absolute paths.  There is even a library function
> os.path.abspath() for converting a relative path to an absolute one.
>
> If you do change cwd during the running of a program, then relative
> filenames that worked earlier might no longer work.  You could convert
> them all to absolute paths, but that's more work.
>
> You can piece together path strings using os.path.join().  It's smart
> enough to know the path separator for your particular platform.
>
> Check out this page:http://docs.python.org/library/os.path.html
>
> > then generate my output? Or should I be doing
> > something else?
>
> > Any and all help is appreciated, thanks!
>
> --
>
> DaveA

Thanks Dave. I am a bit lost as to what the problem is then - the
program runs glitch free, but then only prints: "NOTE *** No xls files
in C:/Documents and Settings/smacdon/." as specified below by my
program. Any idea what the issue might be (my code is below):


import xlrd, xlwt
import glob, os.path
def merge_xls (in_dir, out_file="C:\Documents and Settings\smacdon
\Desktop\09 Aggregate JWS\09_merged_data.xls"):
xls_files   = glob.glob(in_dir + "*.xls")
sheet_names = [os.path.basename(v)[:-4] for v in xls_files]
sheet_excl  = [os.path.basename(v)[:-4] for v in xls_files if len
(os.path.basename(v)[:-4]) > 29]
merged_book = xlwt.Workbook()
if in_dir[-1:] != "/": in_dir = in_dir + "/"
xls_files.sort()
if xls_files:
for k, xls_file in enumerate(xls_files):
print "---> Processing file %s" % (xls_file)
if len (sheet_names[k]) <= 29:
book = xlrd.open_workbook(xls_file)
if book.nsheets == 1:
ws  = merged_book.add_sheet(sheet_names[k])
sheet = book.sheet_by_index(0)
for rx in range(sheet.nrows):
for cx in range(sheet.ncols):
ws.write(rx, cx, sheet.cell_value(rx,
cx))
elif book.nsheets in range(2, 100):
for sheetx in range(book.nsheets):
sheet0n = sheet_names[k]+str(sheetx
+1).zfill(2)
ws = merged_book.add_sheet(sheet0n)
sheet = book.sheet_by_index(sheetx)
for rx in range(sheet.nrows):
for cx in range(sheet.ncols):
ws.write(rx, cx, sheet.cell_value(rx,
cx))
else:
print "ERROR *** File %s has %s sheets (maximum is
99)" % (xls_file, book.nsheets)
raise
else:
print "WARNING *** File name too long: <%s.xls>
(maximum is 29 chars) " % (sheet_names[k])
print "WARNING *** File <%s.xls> was skipped." %
(sheet_names[k])
merged_book.save(out_file)
print
print "---> Merged xls file written to %s using the following
source files: " % (out_file)
for k, v in enumerate(sheet_names):
if len(v) <= 29:
print "\t", str(k+1).zfill(3), "%s.xls" % (v)
print
if sheet_excl:
print "--> The following files were skipped because the
file name exceeds 29 characters: "
for k, v in enumerate(sheet_excl):
print "\t", str(k+1).zfill(3), v
else:
print "NOTE *** No xls files in %s." % (in_dir)
merge_xls(in_dir="C:\Documents and Settings\smacdon\Desktop\09
Aggregate JWS"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading files in from the proper directory

2012-02-07 Thread John Gordon
In  
smac2...@comcast.net writes:

> Am I correct in thinking that I need to change the current working
> directory to this folder in order for Python to read in these files,
> then generate my output?

You don't have to do it that way, no.

In general, when opening a file, you can do it two ways: Either provide
a full pathname, or provide a relative pathname.

If you provide a full pathname (for example "/usr/home/smith/myfile.txt"),
that file will be opened and it does not matter what the current working
directory is.

If you provide a relative pathname (for example "myfile.txt"), python
will attempt to open that file starting from the current working dir.

-- 
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: convert perl-script for voltcraft voltmeter to python [newbie]

2012-02-07 Thread Rick Johnson
On Feb 7, 11:44 am, Dennis Lee Bieber  wrote:
>
> [...]
>
>         Well, since readline() pretty much by definition wants a line-ending
> character before returning, it obviously won't work. (Side comment:
> readline() isn't even shown as part of the basic Serial class -- it is
> in a class FileLike:http://pyserial.sourceforge.net/pyserial_api.html
> -- oh wait.. fine print says that is a base class for Serial on non-io
> module systems).
>
>         What is the boundary marker for your 11-byte chunks? You may need to
> do a synchronization loop using
>
> [...]
>
>         Your timeout is FIFTEEN SECONDS!
>
>         Suspect you want something like

Why do you have this unnatural penchant for superfluous eight space
indention?

I always thought that indenting the first sentence of a paragraph was
quite ridiculous anyhow, however, i cannot even fathom any need to
indent a single sentence! Are you purposely injecting this noise or is
this some automated behavior of your broken mail client? Either way, i
find it annoying and unreadable. Could you please rectify this issue
and bring signal to noise ratio back to reasonable levels?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading files in from the proper directory

2012-02-07 Thread John Gordon
In <9bfb3e39-2bc6-4399-90cc-1c53aa062...@h6g2000yqk.googlegroups.com> 
smac2...@comcast.net writes:

> xls_files   = glob.glob(in_dir + "*.xls")

You may want to put a directory separator character in between the
directory name and the filename glob pattern.

-- 
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: Reading files in from the proper directory

2012-02-07 Thread Peter Otten
smac2...@comcast.net wrote:

> xls_files   = glob.glob(in_dir + "*.xls")

Try changing that to

pattern = os.path.join(in_dir, "*.xls")
xls_files = glob.glob(pattern) 

os.path.join() inserts a (back)slash between directory and filename if 
necessary.

> merge_xls(in_dir="C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS")

If you paste the directory name literal into the interactive interpreter 
you'll be surprised:

>>> "C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS"
'C:\\Documents and Settings\\smacdon\\Desktop\x009 Aggregate JWS'

"\09" is intrpreted as chr(9). Use a raw string to prevent Python from 
interpreting a backslash as the start of an escape sequence

>>> r"C:\Documents and Settings\smacdon\Desktop\09 Aggregate JWS"
'C:\\Documents and Settings\\smacdon\\Desktop\\09 Aggregate JWS'

or use forward slashes as directory separators.

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


Re: iterating over list with one mising value

2012-02-07 Thread Sammy Danso










Hi Expert,
Thanks for your responses and help. thought I should provide more information 
for clarity.
 
Please find the error message below for more information 
 
   for (key, value) in wordFreq2:
ValueError: need more than 1 value to unpack
 
this is a sample of my data
 
['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4, 'two', 1, 
'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1, 'the', 8, 
'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1, 'respectively', 1, 
'received', 1, 'problems', 2, 'prince', 1, 'pregnancy', 1, 'poured', 1, 
'peace', 1, 'pains', 1, 'painless', 1, 'out', 1, 'of', 1, 'noseat', 1, 'nose', 
2, 'no', 2, 'maternity', 1, 'malformation', 1, 'made', 1, 'lower', 1, 
'labour/delivery', 2, 'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 
2, 'hospital', 1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 
'gave', 1, 'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 
'district', 1, 'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 
'cleared', 1, 'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2, 
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad', 1, 
'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2, 'antenatal',
 1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal', 1, '600am', 1, 
'100pm', 1, '', 3, 'other']
 
What I would like to do is to pad the last value 'other' with a default so I 
can iterate sucessfully
 
my desired ouput is the format below in a text file.
with 3
which 3
were 2
..
.
. 
other
  

Thanks again.
Sammy

--- On Tue, 2/7/12, Dave Angel  wrote:


From: Dave Angel 
Subject: Re: iterating over list with one mising value
To: "Sammy Danso" 
Cc: python-list@python.org
Date: Tuesday, February 7, 2012, 2:46 PM


On 02/07/2012 07:27 AM, Sammy Danso wrote:
> Hello experts,
> I am having trouble accessing the content of my list.
> my list content has 2-pair value with the exception of one which has single 
> value. here is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
>   I am unable to iterate through list to access invidual value pairs
>   I get an error message saying ' the list should more than 1 value pairs'. I 
>guess because 'd' has no value. How do I solve this problem?
>   Your help would be much appreciated.
>   Thanks
> Sammy
The real answer is to figure out how it gets into that state.  Is the input 
file invalid?  Or you have a device that sometimes skips one?

But if you're stuck with the data in that list:

If the list is of odd size, truncate it.  Then your loop should not run into an 
uneven pair.

Of course if you actually posted the code, or even the real and complete error 
message, we might be able to make other suggestions.



-- 
DaveA

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


Re: iterating over list with one mising value

2012-02-07 Thread Dave Angel

On 02/07/2012 03:23 PM, Sammy Danso wrote:



Please don't top-post. It hopelessly mixes responses out of order.


Hi Expert,
Thanks for your responses and help. thought I should provide more information 
for clarity.

Please find the error message below for more information

for (key, value) in wordFreq2:
ValueError: need more than 1 value to unpack


That's not the complete error message;  it's missing the call stack. 
And also missing the line that's getting the ValueError.




this is a sample of my data

['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4, 'two', 1, 
'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1, 'the', 8, 
'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1, 'respectively', 1, 
'received', 1, 'problems', 2, 'prince', 1, 'pregnancy', 1, 'poured', 1, 
'peace', 1, 'pains', 1, 'painless', 1, 'out', 1, 'of', 1, 'noseat', 1, 'nose', 
2, 'no', 2, 'maternity', 1, 'malformation', 1, 'made', 1, 'lower', 1, 
'labour/delivery', 2, 'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 
2, 'hospital', 1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 
'gave', 1, 'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 
'district', 1, 'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 
'cleared', 1, 'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2, 
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad', 1, 
'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2, 'antenatal',
  1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal', 1, '600am', 1, 
'100pm', 1, '', 3, 'other']

What I would like to do is to pad the last value 'other' with a default so I 
can iterate sucessfully


OK, good.  If you know your error is always at the end, and you know the 
data to be padded, then just do it:


if len(mylist) % 2 > 0:
 mylist.append(0)



my desired ouput is the format below in a text file.
with 3
which 3
were 2
..
.
.
other


Thanks again.
Sammy


My guess is that you have an entirely different problem, unrelated to 
the missing value at the end.  But you don't show us enough code to help 
you. How many items of the list are processed before the exception happens?


--

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


Re: iterating over list with one mising value

2012-02-07 Thread MRAB

On 07/02/2012 20:23, Sammy Danso wrote:

Hi Expert,
Thanks for your responses and help. thought I should provide more
information for clarity.

>

Please find the error message below for more information




for (key, value) in wordFreq2:
ValueError: need more than 1 value to unpack

this is a sample of my data

['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4,
'two', 1, 'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1,
'the', 8, 'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1,
'respectively', 1, 'received', 1, 'problems', 2, 'prince', 1,
'pregnancy', 1, 'poured', 1, 'peace', 1, 'pains', 1, 'painless', 1,
'out', 1, 'of', 1, 'noseat', 1, 'nose', 2, 'no', 2, 'maternity', 1,
'malformation', 1, 'made', 1, 'lower', 1, 'labour/delivery', 2,
'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 2, 'hospital',
1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 'gave', 1,
'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 'district', 1,
'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 'cleared', 1,
'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2,
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad',
1, 'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2,
'antenatal', 1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal',
1, '600am', 1, '100pm', 1, '', 3, 'other']

What I would like to do is to pad the last value 'other' with a default
so I can iterate sucessfully

my desired ouput is the format below in a text file.
with 3
which 3
were 2
..
.
.
other


The line:

for (key, value) in wordFreq2:

attempts to iterate through the list wordFreq2, where each item in the
list is a _pair_ of values.

However, what you have is a list of _single_ values, alternating string
and number.

That's why it's complaining that it can't unpack.
--
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over list with one mising value

2012-02-07 Thread Aaron France

On 02/07/2012 10:13 PM, MRAB wrote:

On 07/02/2012 20:23, Sammy Danso wrote:

Hi Expert,
Thanks for your responses and help. thought I should provide more
information for clarity.

>

Please find the error message below for more information




for (key, value) in wordFreq2:
ValueError: need more than 1 value to unpack

this is a sample of my data

['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4,
'two', 1, 'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1,
'the', 8, 'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1,
'respectively', 1, 'received', 1, 'problems', 2, 'prince', 1,
'pregnancy', 1, 'poured', 1, 'peace', 1, 'pains', 1, 'painless', 1,
'out', 1, 'of', 1, 'noseat', 1, 'nose', 2, 'no', 2, 'maternity', 1,
'malformation', 1, 'made', 1, 'lower', 1, 'labour/delivery', 2,
'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 2, 'hospital',
1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 'gave', 1,
'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 'district', 1,
'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 'cleared', 1,
'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2,
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad',
1, 'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2,
'antenatal', 1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal',
1, '600am', 1, '100pm', 1, '', 3, 'other']

What I would like to do is to pad the last value 'other' with a default
so I can iterate sucessfully

my desired ouput is the format below in a text file.
with 3
which 3
were 2
..
.
.
other


The line:

for (key, value) in wordFreq2:

attempts to iterate through the list wordFreq2, where each item in the
list is a _pair_ of values.

However, what you have is a list of _single_ values, alternating string
and number.

That's why it's complaining that it can't unpack.

for i in range(0, len(x), 2):
print x[i-1], x[i]

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


Re: iterating over list with one mising value

2012-02-07 Thread Tim Chase

Thanks for your responses and help. thought I should provide
more information for clarity.


It sounds like you want the "grouper" function as defined here:

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

which does what you describe.

-tkc


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


Re: iterating over list with one mising value

2012-02-07 Thread Arnaud Delobelle
On 7 February 2012 20:23, Sammy Danso  wrote:
>
> Hi Expert,
> Thanks for your responses and help. thought I should provide more information 
> for clarity.

Please don't top-post.

> Please find the error message below for more information
>
>    for (key, value) in wordFreq2:
> ValueError: need more than 1 value to unpack
>
> this is a sample of my data
>
> ['with', 3, 'which', 1, [...] , 3, 'other']
>
> What I would like to do is to pad the last value 'other' with a default so I 
> can iterate sucessfully

* It would be better if you provided the code that produces the error,
rather than just the error.  This would allow others to diagnose your
problem without having to guess what you're really doing (see my guess
below)

* Also, don't truncate the traceback.

My guess: you're running a loop like this, where each item is unpacked:

for (key, value) in wordFreq2:
print key, value

on data like that:

wordFreq2 = ['with', 3, 'which', 1, 'were', 2, 'well', 1]

Your list is flat so the unpacking fails.  For it to work, you need
your list to be of the form:

wordFreq2 = [('with', 3), ('which', 1), ('were', 2), ('well', 1)]

Then it will work.  The quickest way to transform your list to the
required form is something like this:

def pairs(seq, fillvalue):
it = iter(seq)
return list(izip_longest(it, it, fillvalue=fillvalue)

so you can do:

word_freq_pairs = pairs(wordFreq2)

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


Re: iterating over list with one mising value

2012-02-07 Thread Chris Angelico
On Wed, Feb 8, 2012 at 8:25 AM, Aaron France
 wrote:
> for i in range(0, len(x), 2):
>    print x[i-1], x[i]

I think you want x[i], x[i+1] here, but in any case, this is a fairly
standard non-Python way to do this sort of thing. There's a variety of
more Pythonic ways to loop, but every now and then, the old C habits
still come in handy!

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


Re: iterating over list with one mising value

2012-02-07 Thread Mark Lawrence

On 07/02/2012 21:25, Aaron France wrote:

for i in range(0, len(x), 2):
 print x[i-1], x[i]

x = ['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4,
'two', 1, 'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1,
'the', 8, 'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1,
'respectively', 1, 'received', 1, 'problems', 2, 'prince', 1,
'pregnancy', 1, 'poured', 1, 'peace', 1, 'pains', 1, 'painless', 1,
'out', 1, 'of', 1, 'noseat', 1, 'nose', 2, 'no', 2, 'maternity', 1,
'malformation', 1, 'made', 1, 'lower', 1, 'labour/delivery', 2,
'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 2, 'hospital',
1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 'gave', 1,
'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 'district', 1,
'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 'cleared', 1,
'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2,
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad',
1, 'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2,
'antenatal', 1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal',
1, '600am', 1, '100pm', 1, '', 3, 'other']
for i in range(0, len(x), 2):
print x[i-1], x[i]

other with
3 which
...
1
3 other

Houston, we've got a problem:)

--
Cheers.

Mark Lawrence.

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


Re: iterating over list with one mising value

2012-02-07 Thread Aaron France

On 02/07/2012 11:09 PM, Mark Lawrence wrote:

On 07/02/2012 21:25, Aaron France wrote:

for i in range(0, len(x), 2):
 print x[i-1], x[i]

x = ['with', 3, 'which', 1, 'were', 2, 'well', 1, 'water', 1, 'was', 4,
'two', 1, 'to', 2, 'through', 1, 'thlabour', 1, 'these', 1, 'theat', 1,
'the', 8, 'tetanus', 1, 'started', 1, 'size', 1, 'scent', 1,
'respectively', 1, 'received', 1, 'problems', 2, 'prince', 1,
'pregnancy', 1, 'poured', 1, 'peace', 1, 'pains', 1, 'painless', 1,
'out', 1, 'of', 1, 'noseat', 1, 'nose', 2, 'no', 2, 'maternity', 1,
'malformation', 1, 'made', 1, 'lower', 1, 'labour/delivery', 2,
'kintampo', 1, 'into', 1, 'injections', 1, 'in', 3, 'i', 2, 'hospital',
1, 'home', 1, 'him', 1, 'having', 1, 'had', 2, 'green', 1, 'gave', 1,
'flowing', 2, 'encountered', 1, 'eleven', 1, 'during', 3, 'district', 1,
'difficulty', 1, 'cord', 1, 'consecutive', 1, 'colour', 1, 'cleared', 1,
'child', 1, 'checkups', 1, 'came', 1, 'but', 2, 'breathing', 2,
'breath', 1, 'blood', 2, 'bleeding', 1, 'birth', 4, 'before', 1, 'bad',
1, 'average', 1, 'at', 2, 'assist', 1, 'artificial', 1, 'around', 2,
'antenatal', 1, 'and', 5, 'an', 1, 'ambrical', 1, 'air', 1, 'abdominal',
1, '600am', 1, '100pm', 1, '', 3, 'other']
for i in range(0, len(x), 2):
print x[i-1], x[i]

other with
3 which
...
1
3 other

Houston, we've got a problem:)


Yupp.

The real problem is that there's been over 20 messages about this 'problem'.


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


Re: iterating over list with one mising value

2012-02-07 Thread Arnaud Delobelle
On 7 February 2012 22:57, Dennis Lee Bieber  wrote:
> On Tue, 7 Feb 2012 21:37:20 +, Arnaud Delobelle 
> wrote:
>
>
>>
>>Your list is flat so the unpacking fails.  For it to work, you need
>>your list to be of the form:
>>
>>    wordFreq2 = [('with', 3), ('which', 1), ('were', 2), ('well', 1)]
>>
>>Then it will work.  The quickest way to transform your list to the
>>required form is something like this:
>>
>        Well... From my viewpoint, the quickest way is to load the list
> correctly in the first place... Where does this list come from? If it's
> a file of

Of course, but how much guessing can you do in a reply?  The OP
provided a Python list, that's what I used.

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


Re: PythonWin debugger holds onto global logging objects too long

2012-02-07 Thread Mark Hammond

On 7/02/2012 9:48 PM, Jean-Michel Pichavant wrote:

Vinay Sajip wrote:

On Jan 24, 2:52 pm, Rob Richardson  wrote:

I use PythonWin to debug the Python scripts we write. Our scripts
often use the log2pyloggingpackage. When running the scripts inside
the debugger, we seem to get oneloggingobject for every time we run
the script. The result is that after running the script five times,
the log file contains five copies of every message. The only way I
know to clean this up and get only a single copy of each message is
to close PythonWin and restart it.

What do I have to do in my scripts to clean up theloggingobjects so
that I never get more than one copy of each message in my log files?



I don't know what log2py is - Google didn't show up anything that
looked relevant. If you're talking about the logging package in the
Python standard library, I may be able to help: but a simple script
that I ran in PythonWin didn't show any problems, so you'll probably
need to post a short script which demonstrates the problem when run in
PythonWin.

Regards,

Vinay Sajip

Same here, can't find anything about log2py.
Anyway it's possible that your pythonwin does not spawn a clean python
interpreter for every run, keeping the same one.


That is what everyone's pythonwin does :)  It always works "in process" 
- not ideal, but also likely to not change.


Cheers,

Mark



So you could possibly keep adding log handlers to your loggers because
they may be static objects (like for the standard logging module).
One solution would be to empty your logger handler list before adding any.

I'm just guessing though, difficult to know without any info on log2py.

JM


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


Re: pySerial question, setting certain serial parameters [newbie]

2012-02-07 Thread Peter
On Feb 4, 11:47 pm, Jean Dupont  wrote:
> I need to set the following options I found in a Perl-script in Python for 
> serial communication with a device (a voltmeter):
>
> $port->handshake("none");
> $port->rts_active(0);
> $port->dtr_active(1);
>
> I have thus far the following  statements but I think it does not set the 
> above parameters correctly:
> import serial
> voltport='/dev/ttyUSB2'
> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,timeout=15)
>
> thanks
> Jean

My reading of the __init__ method documentations shows you should
have:

ser2 = serial.Serial(voltport, 2400, dsrdtr=True, timeout=15)

since the defaults for bytesize are EIGHTBITS (for which you use 8 -
wrong), parity is already default to PARITY_NONE (so this isn't
needed), stopbits defaults to STOPBITS_ONE (for which you use 1 -
wrong) and (assuming the Perl code "1" is to enable) dsrdtr=True
(xonxoff and rtscts both default to False).

Try that.




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


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

2012-02-07 Thread Dietmar Schwertberger

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()
--
http://mail.python.org/mailman/listinfo/python-list


Cycle around a sequence

2012-02-07 Thread Mark Lawrence
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
>>>
--
Cheers.

Mark Lawrence.

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


Re: keeping twisted and wxPython in sync

2012-02-07 Thread crow
On Feb 8, 2:41 am, "Littlefield, Tyler"  wrote:
> Hello all:
> I have a couple questions. First, is there a way to know if connectTCP
> failed? I am writing a client with Twisted and would like to be able to
> notify the user if they couldn't connect.
> Second, I set the protocol on my factory after a connection has been
> made. So when I send my user and password, that is when I connect. Is
> there a way to handle waiting for the connection to complete?
>
> --
>
> Take care,
> Ty
> Web:http://tds-solutions.net
> The Aspen project: a light-weight barebones mud 
> enginehttp://code.google.com/p/aspenmud
>
> Sent from my toaster.

You can send your user & password in connectionMade() method, I think.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: keeping twisted and wxPython in sync

2012-02-07 Thread crow
On Feb 8, 2:41 am, "Littlefield, Tyler"  wrote:
> Hello all:
> I have a couple questions. First, is there a way to know if connectTCP
> failed? I am writing a client with Twisted and would like to be able to
> notify the user if they couldn't connect.
> Second, I set the protocol on my factory after a connection has been
> made. So when I send my user and password, that is when I connect. Is
> there a way to handle waiting for the connection to complete?
>
> --
>
> Take care,
> Ty
> Web:http://tds-solutions.net
> The Aspen project: a light-weight barebones mud 
> enginehttp://code.google.com/p/aspenmud
>
> Sent from my toaster.

And for connection failed, you can write some hook code in
connectionLost() method, this method will be called when connection
failed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cycle around a sequence

2012-02-07 Thread Terry Reedy

On 2/7/2012 8:10 PM, 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


a=range(10)
n = len(a)
off = 4
for k in (a[(i+off) % n] for i in range(n)):
  print(a[k], end = ' ')

--
Terry Jan Reedy

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


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

2012-02-07 Thread Lei Cheng
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over list with one mising value

2012-02-07 Thread Steven D'Aprano
(Apologies in advance for breaking threading, but the original post in 
this thread doesn't appear for me.)

> On Tue, Feb 7, 2012 at 5:27 AM, Sammy Danso 
> wrote:
>>
>> Hello experts,
>> I am having trouble accessing the content of my list. my list content
>> has 2-pair value with the exception of one which has single value. here
>> is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
>>
>> I am unable to iterate through list to access invidual value pairs


Here's a recipe to collate a sequence of interleaved items. It collects 
every nth item:


from itertools import islice, tee

def collate(iterable, n):
t = tee(iter(iterable), n)
slices = (islice(it, i, None, n) for (i, it) in enumerate(t))
return [list(slice) for slice in slices]


And here it is in use:


>>> seq = [1, 'a', 'A', 2, 'b', 'B', 3, 'c', 'C', 4, 'd', 'D', 5, 'e']
>>> collate(seq, 3)
[[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e'], ['A', 'B', 'C', 'D']]
>>> collate(seq, 2)
[[1, 'A', 'b', 3, 'C', 'd', 5], ['a', 2, 'B', 'c', 4, 'D', 'e']]
>>> collate(seq, 9)
[[1, 4], ['a', 'd'], ['A', 'D'], [2, 5], ['b', 'e'], ['B'], [3], ['c'], 
['C']]


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


Re: Cycle around a sequence

2012-02-07 Thread Christoph Hansen

Mark Lawrence schrieb:

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?


# quick&dirty

seq=range(10)
for x in seq[4:]+seq[:4]:
print x

# or

def oneround(seq, start=0):
i=start
l=len(seq)
while True:
yield seq[i]
i = (i+1) % l
if i==start: break

for x in oneround(range(50), 4):
print x





--
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-07 Thread Dave Angel

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

--
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-07 Thread Patto
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?
-- 
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-07 Thread Dave Angel
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.


--

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


Re: python file synchronization

2012-02-07 Thread Cameron Simpson
On 07Feb2012 01:33, silentnights  wrote:
| I have the following problem, I have an appliance (A) which generates
| records and write them into file (X), the appliance is accessible
| throw ftp from a server (B). I have another central server (C) that
| runs a Django App, that I need to get continuously the records from
| file (A).
| 
| The problems are as follows:
| 1. (A) is heavily writing to the file, so copying the file will result
| of uncompleted line at the end.
| 2. I have many (A)s and (B)s  that I need to get the data from.
| 3. I can't afford losing any records from file (X)
[...]
| The above is implemented and working, the problem is that It required
| so many syncs and has a high overhead and It's hard to debug.

Yep.

I would change the file discipline. Accept that FTP is slow and has no
locking. Accept that reading records from an actively growing file is
often tricky and sometimes impossible depending on the record format.
So don't. Hand off completed files regularly and keep the incomplete
file small.

Have (A) write records to a file whose name clearly shows the file to be
incomplete. Eg "data.new". Every so often (even once a second), _if_ the
file is not empty: close it, _rename_ to "data.timestamp" or
"data.sequence-number", open a new "data.new" for new records. 

Have the FTP client fetch only the completed files.

You can perform a similar effort for the socket daemon: look only for
completed data files. Reading the filenames from a directory is very
fast if you don't stat() them (i.e. just os.listdir). Just open and scan
any new files that appear.

That would be my first cut.
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Performing random acts of moral ambiguity.
- Jeff Miller 
-- 
http://mail.python.org/mailman/listinfo/python-list


turbogears 1

2012-02-07 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. Am I basically given
vaporware? Does anyone have any up to date info?

Best,
Hung

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


Re: turbogears 1

2012-02-07 Thread Roy Smith
In article ,
 anon hung  wrote:

> 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

> Am I basically given vaporware? Does anyone have any up to date info?

Have you considered Ruby on Rails?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrypto builds neither with MSVC nor MinGW

2012-02-07 Thread Alec Taylor
Thanks all for your replies.

I have now installed MSVC8 and YASM.

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?

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: turbogears 1

2012-02-07 Thread Steven D'Aprano
On Wed, 08 Feb 2012 00:33:55 -0500, Roy Smith wrote:

> In article ,
>  anon hung  wrote:
> 
>> 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?


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

Now that's just cruel.


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


Issue with Scrapping Data from a webpage- Noob Question

2012-02-07 Thread abhijeet mahagaonkar
Hi Fellow Pythoners,

I'm trying to collect table data from an authenticated webpage (Tool) to
which I have access.

I will have the required data after 'click'ing a submit button on the tool
homepage.
When I inspect the submit button i see


Thus the tool's homepage is of the form www.example.com/Tool and on
clicking the submit button the data I need will be at
www.example.com/Tool/index.do

The problem that I'm running into is in my below code is giving me the
source of homepage(www.example.com/Tool) and not the of the submitted page(
www.example.com/Tool/index.do)

url="www.example.com/Tool/index.do"
request = urllib2.Request(url, data, {'Authorization': "Basic " +
base64.b64encode("%s:%s" % (username, password))})
Response_Page=urllib2.urlopen(request).read()

Is there a way I can access the source of the submitted page?

PS: Sorry for laying out very tiny details on what I'm trying to do, I just
wanted to explain myself clearly :)

Thanks in advance for your time on this one.

Warm Regards,
Abhi
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python file synchronization

2012-02-07 Thread Sherif Shehab Aldin
Hi Cameron,

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.

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.

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.

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

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.

On Wed, Feb 8, 2012 at 5:40 AM, Cameron Simpson  wrote:

> On 07Feb2012 01:33, silentnights  wrote:
> | I have the following problem, I have an appliance (A) which generates
> | records and write them into file (X), the appliance is accessible
> | throw ftp from a server (B). I have another central server (C) that
> | runs a Django App, that I need to get continuously the records from
> | file (A).
> |
> | The problems are as follows:
> | 1. (A) is heavily writing to the file, so copying the file will result
> | of uncompleted line at the end.
> | 2. I have many (A)s and (B)s  that I need to get the data from.
> | 3. I can't afford losing any records from file (X)
> [...]
> | The above is implemented and working, the problem is that It required
> | so many syncs and has a high overhead and It's hard to debug.
>
> Yep.
>
> I would change the file discipline. Accept that FTP is slow and has no
> locking. Accept that reading records from an actively growing file is
> often tricky and sometimes impossible depending on the record format.
> So don't. Hand off completed files regularly and keep the incomplete
> file small.
>
> Have (A) write records to a file whose name clearly shows the file to be
> incomplete. Eg "data.new". Every so often (even once a second), _if_ the
> file is not empty: close it, _rename_ to "data.timestamp" or
> "data.sequence-number", open a new "data.new" for new records.
>
> Have the FTP client fetch only the completed files.
>
> You can perform a similar effort for the socket daemon: look only for
> completed data files. Reading the filenames from a directory is very
> fast if you don't stat() them (i.e. just os.listdir). Just open and scan
> any new files that appear.
>
> That would be my first cut.
> --
> Cameron Simpson  DoD#743
> http://www.cskk.ezoshosting.com/cs/
>
> Performing random acts of moral ambiguity.
>- Jeff Miller 
>
-- 
http://mail.python.org/mailman/listinfo/python-list