Re: Some help in refining this regex for CSV files

2012-12-06 Thread Mark Lawrence

On 06/12/2012 07:21, Oltmans wrote:

Hi guys,

I've to deal with CSVs that look like following

CSV (with one header and 3 legit rows where each legit row has 3 columns)

Some info
Date: 12/6/2012
Author: Some guy
Total records: 100

header1, header2, header3
one, two, three
one, "Python is great, so are other languages, isn't ?", three
one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n 
this \t\t\t fact. \t\t\t\tthis fact alone is amazing'


So inside this CSV, there will always be bad lines like the top 4 (they could 
end up in the beginning, in the middle and even in the last). So above sample, 
csv has 3 legit lines and a header. I want to read those three lines and here 
is a regex that I came up with (which clearly isn't working)

 #print line
 pattern = r"([^\t]+\t|,+)"
 matches = re.match(pattern, line)

Do you've any better ideas guys? I will really appreciate all help.



I'd simply use the csv module from the standard library to read your 
files, discarding anything that you regard as bad.  I'd certainly not 
use a regex for this.


--
Cheers.

Mark Lawrence.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Vlastimil Brom
2012/12/5 Nick Mellor :
> Neil,
>
> Further down the data, found another edge case:
>
> "Spring ONION from QLD"
>
> Following the spec, the whole line should be description (description starts 
> at first word that is not all caps.) This case breaks the latest groupby.
>
> N
> --
> http://mail.python.org/mailman/listinfo/python-list

Hi,
Just for completeness..., it (likely) can be done using regex (given
the current specificatioin), but if the data are even more complex and
varying, the tools like pyparsing or dedicated parsing functions might
be more appropriate;

hth,
   vbr:


>>> import re
>>> test_product_data = """BEANS hand picked
... BEETROOT certified organic
... BOK CHOY (bunch)
... BROCCOLI Mornington Peninsula
... BRUSSEL  SPROUTS
... CABBAGE green
... CABBAGE Red
... CAPSICUM RED
... CARROTS
... CARROTS loose
... CARROTS juicing, certified organic
... CARROTS Trentham, large seconds, certified organic
... CARROTS Trentham, firsts, certified organic
... CAULIFLOWER
... CELERY Mornington Peninsula IPM grower
... CELERY Mornington Peninsula IPM grower
... CUCUMBER
... EGGPLANT
... FENNEL
... GARLIC (from Argentina)
... GINGER fresh uncured
... KALE (bunch)
... KOHL RABI certified organic
... LEEKS
...  LETTUCE iceberg
... MUSHROOM cup or flat
... MUSHROOM Swiss brown
... ONION brown
... ONION red
... ONION spring (bunch)
... PARSNIP, certified organic
... POTATOES certified organic
... POTATOES Sebago
... POTATOES Desiree
... POTATOES Bullarto chemical free
... POTATOES Dutch Cream
... POTATOES Nicola
... POTATOES Pontiac
... POTATOES Otway Red
... POTATOES teardrop
... PUMPKIN certified organic
... SCHALLOTS brown
... SNOW PEAS
... SPINACH I'll try to get certified organic (bunch)
... SWEET POTATO gold certified organic
... SWEET POTATO red small
... SWEDE certified organic
... TOMATOES  Qld
... TURMERIC fresh certified organic
... ZUCCHINI
... APPLES Harcourt  Pink Lady, Fuji, Granny Smith
... APPLES Harcourt 2 kg bags, Pink Lady or Fuji (bag)
... AVOCADOS
... AVOCADOS certified organic, seconds
... BANANAS Qld, organic
... GRAPEFRUIT
... GRAPES crimson seedless
... KIWI FRUIT Qld certified organic
... LEMONS
... LIMES
... MANDARINS
... ORANGES Navel
... PEARS Beurre Bosc Harcourt new season
... PEARS Packham, Harcourt new season
... SULTANAS 350g pre-packed bags
... EGGS Melita free range, Barker's Creek
... BASIL (bunch)
... CORIANDER (bunch)
... DILL (bunch)
... MINT (bunch)
... PARSLEY (bunch)
... Spring ONION from QLD"""
>>>
>>> len(test_product_data.splitlines())
72
>>>
>>> for prod_item in re.findall(r"(?m)(?=^.+$)^ *(?:([A-Z ]+\b(?>> )(?=[\s,]|$)))?(?: *(.*))?$", test_product_data): print prod_item
...
('BEANS', 'hand picked')
('BEETROOT', 'certified organic')
('BOK CHOY', '(bunch)')
('BROCCOLI', 'Mornington Peninsula')
('BRUSSEL  SPROUTS', '')
('CABBAGE', 'green')
('CABBAGE', 'Red')
('CAPSICUM RED', '')
('CARROTS', '')
('CARROTS', 'loose')
('CARROTS', 'juicing, certified organic')
('CARROTS', 'Trentham, large seconds, certified organic')
('CARROTS', 'Trentham, firsts, certified organic')
('CAULIFLOWER', '')
('CELERY', 'Mornington Peninsula IPM grower')
('CELERY', 'Mornington Peninsula IPM grower')
('CUCUMBER', '')
('EGGPLANT', '')
('FENNEL', '')
('GARLIC', '(from Argentina)')
('GINGER', 'fresh uncured')
('KALE', '(bunch)')
('KOHL RABI', 'certified organic')
('LEEKS', '')
('LETTUCE', 'iceberg')
('MUSHROOM', 'cup or flat')
('MUSHROOM', 'Swiss brown')
('ONION', 'brown')
('ONION', 'red')
('ONION', 'spring (bunch)')
('PARSNIP', ', certified organic')
('POTATOES', 'certified organic')
('POTATOES', 'Sebago')
('POTATOES', 'Desiree')
('POTATOES', 'Bullarto chemical free')
('POTATOES', 'Dutch Cream')
('POTATOES', 'Nicola')
('POTATOES', 'Pontiac')
('POTATOES', 'Otway Red')
('POTATOES', 'teardrop')
('PUMPKIN', 'certified organic')
('SCHALLOTS', 'brown')
('SNOW PEAS', '')
('SPINACH', "I'll try to get certified organic (bunch)")
('SWEET POTATO', 'gold certified organic')
('SWEET POTATO', 'red small')
('SWEDE', 'certified organic')
('TOMATOES', 'Qld')
('TURMERIC', 'fresh certified organic')
('ZUCCHINI', '')
('APPLES', 'Harcourt  Pink Lady, Fuji, Granny Smith')
('APPLES', 'Harcourt 2 kg bags, Pink Lady or Fuji (bag)')
('AVOCADOS', '')
('AVOCADOS', 'certified organic, seconds')
('BANANAS', 'Qld, organic')
('GRAPEFRUIT', '')
('GRAPES', 'crimson seedless')
('KIWI FRUIT', 'Qld certified organic')
('LEMONS', '')
('LIMES', '')
('MANDARINS', '')
('ORANGES', 'Navel')
('PEARS', 'Beurre Bosc Harcourt new season')
('PEARS', 'Packham, Harcourt new season')
('SULTANAS', '350g pre-packed bags')
('EGGS', "Melita free range, Barker's Creek")
('BASIL', '(bunch)')
('CORIANDER', '(bunch)')
('DILL', '(bunch)')
('MINT', '(bunch)')
('PARSLEY', '(bunch)')
('', 'Spring ONION from QLD')
>>> len(re.findall(r"(?m)(?=^.+$)^ *(?:([A-Z ]+\b(?>> *(.*))?$", test_product_data))
72
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread Bruno Dupuis
On Thu, Dec 06, 2012 at 04:32:34AM +, Steven D'Aprano wrote:
> On Thu, 06 Dec 2012 03:22:53 +, Rotwang wrote:
> 
> > On 06/12/2012 00:19, Bruno Dupuis wrote:
> >> [...]
> >>
> >> Another advice: never ever
> >>
> >> except XXXError:
> >>  pass
> >>
> >> at least log, or count, or warn, or anything, but don't pass.
> > 
> > Really? I've used that kind of thing several times in my code. For
> > example, there's a point where I have a list of strings and I want to
> > create a list of those ints that are represented in string form in my
> > list, so I do this:
> > 
> > listofints = []
> > for k in listofstrings:
> > try:
> > listofints.append(int(k))
> > except ValueError:
> > pass
> > 
> > Another example: I have a dialog box with an entry field where the user
> > can specify a colour by entering a string, and a preview box showing the
> > colour. I want the preview to automatically update when the user has
> > finished entering a valid colour string, so whenever the entry field is
> > modified I call this:
> > 
> > def preview(*args):
> > try:
> > previewbox.config(bg = str(entryfield.get()))
> > except tk.TclError:
> > pass
> > 
> > Is there a problem with either of the above? If so, what should I do
> > instead?
> 
> They're fine.
> 
> Never, ever say that people should never, ever do something.
> 
> 
> *cough*
> 

Well, dependening on the context (who provides listofstrings?) I would
log or count errors on the first one... or not.

On the second one, I would split the expression, because (not sure of
that point, i didn't import tk for years) previewbox.config and
entryfield.get may raise a tk.TclError for different reasons.

The point is Exceptions are made for error handling, not for normal
workflow. I hate when i read that for example:

try:
do_stuff(mydict[k])
except KeyError:
pass

(loads of them in many libraries and frameworks)
instead of:

if k in mydict:
do_stuff(mydict[k])

Note that the performances are better with the latter.

There are some exceptions to this, though, like StopIteration

For me, it's a rule of thumb, except: pass is possible in situations
where I control every input data, and I deeply, exactly know all code
interractions. If figuring all this out is longer (it's almost always
the case) than typing:

log.warning('oops:\n %s' % traceback.format_exc())

I log. 

It depends also on the context, I'd be more 'permissive' a short
script than into a large program, framework, or lib, for the
very reason it's easy to know all code interactions.

In my coder life, i spent more time debugging silently swallowed exceptions
than logging abnormal behaviours.

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


Re: Confused compare function :)

2012-12-06 Thread Chris Angelico
On Thu, Dec 6, 2012 at 7:49 PM, Bruno Dupuis
 wrote:
>
> The point is Exceptions are made for error handling, not for normal
> workflow. I hate when i read that for example:
>
> try:
> do_stuff(mydict[k])
> except KeyError:
> pass
>
> (loads of them in many libraries and frameworks)
> instead of:
>
> if k in mydict:
> do_stuff(mydict[k])
>
> Note that the performances are better with the latter.
>

This is the age-old question of EAFP vs LBYL.

The check-first "Look Before You Leap" option has a small chance of
race condition. If something changes between the 'if' and the usage,
maybe from another thread or maybe a signal handler or perhaps some
object's __del__ method gets called or who knows what, you'll have a
problem.

Python's usual philosophy is that it's Easier to Ask Forgiveness than
Permission. Just do it, and jump out if you can't. This technique
plays *very* nicely with generic handlers. For instance, at work I
wrote an HTTP daemon that's supposed to receive XML-encoded POST data
with a particular structure. The handler function (called once for
every HTTP request) looks something like this, in Pythonesque
pseudocode:

def handler(req):
  try:
msg = parse_xml(req.body) # returns a dict of dicts/lists/strings
stuff = 
msg["soapenv:Envelope"]["soapenv:Body"]["GetItemTransactionsResponse"]
sig = stuff["Item"]["ApplicationData"]
if sig.does.not.match(): return "Bad request"
sscanf(sig,"FOO %d %d %d",account,table,row)
accountdata[account][table][row] = 1
return "Done and successful."
  except:
log_error_to_stderr()
return "Done."

I don't particularly care _what_ the error is. Most of the time, I
won't even bother to look at the log file (it's run via an Upstart
job, and stderr is redirected to a file), but if I'm having problems,
I can go check. Generally, exceptions thrown by that code are the
result of malformed info packets; since it's basic HTTP, it's easy for
anyone to send a request in, and I don't care to see those errors
logged. In fact, the logging to stderr can even get commented out in
production, and used only when there's actually a problem being
diagnosed.

To try to handle all possible errors in that code by LBLY, I would
need to pepper the code with conditions and an appropriate 'return'
statement (and, though the pseudo-code has the function returning a
string, the actual code involves an explicit "send this response"
call). Plus, I'd need to predict every possible failure. With EAFP,
all I need is one simple "catch" handler for the whole block of code,
and I can easily eyeball just a few function exit points to see that
an appropriate HTTP response will always be sent.

Each has its place.

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


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
On Thu, Dec 6, 2012 at 1:31 AM, Steven D'Aprano
 wrote:
> On Wed, 05 Dec 2012 23:50:49 +0100, Anatoli Hristov wrote:
>
>
>> def Change_price():
>
> Misleading function name. What price does it change?
>
>
>> total = 0
>> tnf = 0
>
> "tnf"? Does that mean something?
>
>
>> for row in DB:  # DB is mySQL DB, logically I get out
>> # 1 SKU and I compare it with next loop
>
> Use of global variables, yuck. What happens if some day you need two
> databases at the same time?
>
>
>> isku = row["sku"]
>> isku = isku.lower()
>
> Hungarian Notation? This is better written as:
>
> sku = row["sku"].lower()
>
>
>> iprice = row["price"]
>> iprice = int(iprice)
>
> And likewise price = int(row["price"]). Or better still, "oldprice", or
> "price_in_database", or something that actually describes what it is.
>
>
>> found = 0
>
> found = False
>
>
>> try:
>> for x in PRICELIST:  # here is my next loop in a CSV file
>>  # which is allready in a list PRICELIST
>> try:
>> dprice = x[6]
>
> "dprice"? D-for-database price? But this is the price from the CSV file,
> not from the database. Another misleading name, leading to confusion.
>
>
>> dprice = dprice.replace(",",".")
>> # As in the PRICELIST the prices are with
>> # commas I replace the comma as python request it
>> dprice = float(dprice)
>> newprice = round(dprice)*1.10
>> dsku = x[4]
>> dsku = dsku.lower()
>
> And again, what's "dsku" mean? Database-SKU? But it's the CSV SKU.
>
>
>> stock = int(x[7])
>
> I don't believe that this is used at all. Get rid of it.
>
>
>> if isku == dsku and newprice < int(iprice):
>> # If found the coresponded SKU and the price is
>> # higher than the one in the CSV I update the price
>
> I think your logic is wrong here. You aren't comparing the price in the
> CSV here at all. You compare two prices, neither of which is the price in
> the CSV file:
>
> newprice = round(price in CSV) * 1.10
> iprice = price from the database
>
> (which is already an int, no need to call int *again* -- if you're going
> to use Hungarian Notation, pay attention to it!)
>
> So you have THREE prices, not two, and it isn't clear which ones you are
> *supposed* to compare. Either the code is wrong, or the comment is wrong,
> or possibly both.
>
> iprice = price from the database
> dprice = price from the CSV
> newprice = calculated new price
>
> I'm going to GUESS that you actually want to compare the new price with
> the price in the database.
>
> if newprice > iprice:  # horrible name! no wonder you are confused
> # Update the database with the new price
>
>
>> print dsku, x[6], dprice, newprice
>> Update_SQL(newprice, isku)
>> # goes to the SQL Update
>
> Really? Gosh, without the comment, how would anyone know that Update_SQL
> updates the SQL? :-P
>
> Seriously, the comment is redundant. Get rid of it.
>
>
>> print isku, newprice
>> if isku == dsku:
>> # Just a check to see if it works
>> print "Found %s" %dsku
>> found = 1
>> else:
>> found = 0
>
> found = True or False is better.
>
> But this code cannot do anything but print Found, since above you already
> tested that isku == dsku. So this check is pointless.
>
> The reason is, your code does this:
>
> if isku == dsku and (something else):
> # Inside this block, isku MUST equal dsku
> blah blah blah
> if isku == dsku:
> print "Found"
> found = 1
> else:
> # But this cannot possibly happen
> print "not found"
> found = 0
>
>
>> except IndexError:
>> pass
>> except ValueError:
>> pass
>> except TypeError:
>> pass
>
> Why are you hiding errors? You should not hide errors unnecessarily, that
> means there are bugs in either the CSV or your code, you should fix the
> bugs.
>
> However, if you really must, then you can replace all of those with:
>
>   except (IndexError, ValueError, TypeError):
>   pass
>
>
>> except IndexError:
>> pass
>
> And hiding more errors?
>
>
>> if found == 1:
>> print "%s This is match" % isku
>> if found == 0:
>> print "%s Not found" % isku
>> tnf = tnf +1
>> total = total +1
>
> Better to write this as:
>
> if found:
>print "%s This is match" % isku
> else:
>print "%s Not found" % isku

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

2012-12-06 Thread iMath
the following code originally from 
http://zetcode.com/databases/mysqlpythontutorial/
within the "Writing images" part .


import MySQLdb as mdb
import sys

try:
fin = open("Chrome_Logo.svg.png",'rb')
img = fin.read()
fin.close()

except IOError as e:

print ("Error %d: %s" % (e.args[0],e.args[1]))
sys.exit(1)


try:
conn = mdb.connect(host='localhost',user='testuser',
   passwd='test623', db='testdb')
cursor = conn.cursor()
cursor.execute("INSERT INTO Images SET Data='%s'" % \
mdb.escape_string(img))

conn.commit()

cursor.close()
conn.close()

except mdb.Error as e:

print ("Error %d: %s" % (e.args[0],e.args[1]))
sys.exit(1)


I port it to python 3 ,and also change 
fin = open("chrome.png") 
to 
fin = open("Chrome_Logo.png",'rb')
but when I run it ,it gives the following error :

Traceback (most recent call last): 
  File "E:\Python\py32\itest4.py", line 20, in 
mdb.escape_string(img))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid 
start byte

so how to fix it ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

2012-12-06 Thread Terry Reedy

On 12/6/2012 5:07 AM, iMath wrote:

the following code originally from 
http://zetcode.com/databases/mysqlpythontutorial/
within the "Writing images" part .


import MySQLdb as mdb


Not part of stdlib. 'MySQLdb' should be in the subject line to get 
attention of someone who is familiar with it. I am not.



import sys

try:
 fin = open("Chrome_Logo.svg.png",'rb')
 img = fin.read()
 fin.close()

except IOError as e:

 print ("Error %d: %s" % (e.args[0],e.args[1]))
 sys.exit(1)


try:
 conn = mdb.connect(host='localhost',user='testuser',
passwd='test623', db='testdb')
 cursor = conn.cursor()
 cursor.execute("INSERT INTO Images SET Data='%s'" % \
 mdb.escape_string(img))


From the name, I would expect that excape_string expects text. From the 
error, it seems to specifically expect utf-8 encoded bytes. After 
decoding, I expect that it does some sort of 'escaping'. An image does 
not qualify as that sort of input. If escape_string takes an encoding 
arg, latin1 *might* work.



 conn.commit()

 cursor.close()
 conn.close()

except mdb.Error as e:

 print ("Error %d: %s" % (e.args[0],e.args[1]))
 sys.exit(1)


I port it to python 3 ,and also change
fin = open("chrome.png")
to
fin = open("Chrome_Logo.png",'rb')
but when I run it ,it gives the following error :

Traceback (most recent call last):
   File "E:\Python\py32\itest4.py", line 20, in 
 mdb.escape_string(img))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid 
start byte

so how to fix it ?




--
Terry Jan Reedy

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


RE: Dict comprehension help

2012-12-06 Thread Peter Otten
Joseph L. Casale wrote:

[Ian Kelly]
>> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()}
> 
> Ugh, had part of that backwards:) Nice!
> 
>> However, since you say that all dicts have a unique value for
>> z['key'], you should never need to actually merge two dicts, correct?
>> In that case, why not just use a plain for loop to search for the
>> dict?
> 
> The reason is that I need several of these in an init function and they
> stack up nicely where as the loops get unruly in length, but I don't
> disagree.

You could put the loop into a helper function, but if you are looping 
through the same my_list more than once why not build a lookup table

my_dict = {d["key"]: d for d in my_list}

and then find the required dict with

my_dict[value]

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

2012-12-06 Thread Hans Mulder
On 6/12/12 11:07:51, iMath wrote:
> the following code originally from 
> http://zetcode.com/databases/mysqlpythontutorial/
> within the "Writing images" part .
> 
> 
> import MySQLdb as mdb
> import sys
> 
> try:
> fin = open("Chrome_Logo.svg.png",'rb')
> img = fin.read()
> fin.close()
> 
> except IOError as e:
> 
> print ("Error %d: %s" % (e.args[0],e.args[1]))
> sys.exit(1)
> 
> 
> try:
> conn = mdb.connect(host='localhost',user='testuser',
>passwd='test623', db='testdb')
> cursor = conn.cursor()
> cursor.execute("INSERT INTO Images SET Data='%s'" % \
> mdb.escape_string(img))

You shouldn't call mdb.escape_string directly.  Instead, you
should put placeholders in your SQL statement and let MySQLdb
figure out how to properly escape whatever needs escaping.

Somewhat confusingly, placeholders are written as %s in MySQLdb.
They differ from strings in not being enclosed in quotes.
The other difference is that you'd provide two arguments to
cursor.execute; the second of these is a tuple; in this case
a tuple with only one element:

cursor.execute("INSERT INTO Images SET Data=%s", (img,))

> conn.commit()
> 
> cursor.close()
> conn.close()
> 
> except mdb.Error as e:
> 
> print ("Error %d: %s" % (e.args[0],e.args[1]))
> sys.exit(1)
> 
> 
> I port it to python 3 ,and also change 
> fin = open("chrome.png") 
> to 
> fin = open("Chrome_Logo.png",'rb')
> but when I run it ,it gives the following error :
> 
> Traceback (most recent call last): 
>   File "E:\Python\py32\itest4.py", line 20, in 
> mdb.escape_string(img))
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: 
> invalid start byte
> 
> so how to fix it ?

Python 3 distinguishes between binary data and Unicode text.
Trying to apply string functions to images or other binary
data won't work.

Maybe correcting this bytes/strings confusion and porting
to Python 3 in one go is too large a transformation.  In
that case, your best bet would be to go back to Python 2
and fix all the bytes/string confusion there.  When you've
got it working again, you may be ready to port to Python 3.


Hope this helps,

-- HansM

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

2012-12-06 Thread Steven D'Aprano
On Thu, 06 Dec 2012 02:07:51 -0800, iMath wrote:

> the following code originally from
> http://zetcode.com/databases/mysqlpythontutorial/ within the "Writing
> images" part .
> 
> 
> import MySQLdb as mdb
> import sys
> 
> try:
> fin = open("Chrome_Logo.svg.png",'rb') 
> img = fin.read()
> fin.close()
> except IOError as e:
> print ("Error %d: %s" % (e.args[0],e.args[1]))
> sys.exit(1)

Every time a programmer catches an exception, only to merely print a 
vague error message and then exit, God kills a kitten. Please don't do 
that.

If all you are going to do is print an error message and then exit, 
please don't bother. All you do is make debugging harder. When Python 
detects an error, by default it prints a full traceback, which gives you 
lots of information to track down the error. By catching that exception 
as you do, you lose that information and make it harder to debug.

Moving on to the next thing:


[snip code]
> I port it to python 3 ,and also change fin = open("chrome.png")
> to
> fin = open("Chrome_Logo.png",'rb')
> but when I run it ,it gives the following error :
> 
> Traceback (most recent call last):
>   File "E:\Python\py32\itest4.py", line 20, in 
> mdb.escape_string(img))
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0:
> invalid start byte
> 
> so how to fix it ?

I suggest you start by reading the documentation for 
MySQLdb.escape_string. What does it do? What does it expect? A byte 
string or a unicode text string?

It seems very strange to me that you are reading a binary file, then 
passing it to something which appears to be expecting a string. It looks 
like what happens is that the PNG image starts with a 0x89 byte, and the 
escape_string function tries to decode those bytes into Unicode text:

py> img = b"\x89\x00\x23\xf2"  # fake PNG binary data
py> img.decode('utf-8')  # I'm expecting text
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: 
invalid start byte

Without knowing more about escape_string, I can only make a wild guess. 
Try this:

import base64
img = fin.read()  # read the binary data of the PNG file
data = base64.encodebytes(img)  # turn the binary image into text
cursor.execute("INSERT INTO Images SET Data='%s'" % \
mdb.escape_string(data))


and see what that does.


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


Re: Confused compare function :)

2012-12-06 Thread Steven D'Aprano
On Thu, 06 Dec 2012 09:49:26 +0100, Bruno Dupuis wrote:

> The point is Exceptions are made for error handling, not for normal
> workflow. 

That's certainly not the case in Python. Using exceptions for flow 
control is a standard part of the language.

IndexError and StopIteration are used to detect the end of lists or 
iterators in for loops.

GeneratorExit is used to request that generators exit.

SysExit is used to exit the interpreter.


There is nothing wrong with using exceptions for flow control in 
moderation.


> I hate when i read that for example:
> 
> try:
> do_stuff(mydict[k])
> except KeyError:
> pass
> 
> (loads of them in many libraries and frameworks) instead of:
> 
> if k in mydict:
> do_stuff(mydict[k])
> 
> Note that the performances are better with the latter.

Not so. Which one is faster will depend on how often you expect to fail. 
If the keys are nearly always present, then:

try:
do_stuff(mydict[k])
except KeyError:
pass

will be faster. Setting up a try block is very fast, about as fast as 
"pass", and faster than "if k in mydict".

But if the key is often missing, then catching the exception will be 
slow, and the "if k in mydict" version may be faster. It depends on how 
often the key is missing.


[...]
> It depends also on the context, I'd be more 'permissive' a short script
> than into a large program, framework, or lib, for the very reason it's
> easy to know all code interactions.
> 
> In my coder life, i spent more time debugging silently swallowed
> exceptions than logging abnormal behaviours.


That's fine. I agree with you about not silently swallowing errors. Where 
I disagree is that you said "never ever", which is an exaggeration. 
Remember that exceptions are not always errors.

Problem: take a list of strings, and add up the ones which are integers, 
ignoring everything else.

Solution:

total = 0
for s in list_of_strings:
try:
total += int(s)
except ValueError:
pass  # Not a number, ignore it.


Why would you want to log that? It's not an error, it is working as 
designed. I hate software that logs every little thing that happens, so 
that you cannot tell what's important and what isn't.



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


Re: Confused compare function :)

2012-12-06 Thread peter

On 12/06/2012 08:47 AM, Steven D'Aprano wrote:

On Thu, 06 Dec 2012 09:49:26 +0100, Bruno Dupuis wrote:


The point is Exceptions are made for error handling, not for normal
workflow.

That's certainly not the case in Python. Using exceptions for flow
control is a standard part of the language.

IndexError and StopIteration are used to detect the end of lists or
iterators in for loops.

GeneratorExit is used to request that generators exit.

SysExit is used to exit the interpreter.


There is nothing wrong with using exceptions for flow control in
moderation.



I hate when i read that for example:

 try:
 do_stuff(mydict[k])
 except KeyError:
 pass

(loads of them in many libraries and frameworks) instead of:

 if k in mydict:
 do_stuff(mydict[k])

Note that the performances are better with the latter.

Not so. Which one is faster will depend on how often you expect to fail.
If the keys are nearly always present, then:

try:
 do_stuff(mydict[k])
except KeyError:
 pass

will be faster. Setting up a try block is very fast, about as fast as
"pass", and faster than "if k in mydict".

But if the key is often missing, then catching the exception will be
slow, and the "if k in mydict" version may be faster. It depends on how
often the key is missing.


[...]

It depends also on the context, I'd be more 'permissive' a short script
than into a large program, framework, or lib, for the very reason it's
easy to know all code interactions.

In my coder life, i spent more time debugging silently swallowed
exceptions than logging abnormal behaviours.


That's fine. I agree with you about not silently swallowing errors. Where
I disagree is that you said "never ever", which is an exaggeration.
Remember that exceptions are not always errors.

Problem: take a list of strings, and add up the ones which are integers,
ignoring everything else.

Solution:

total = 0
for s in list_of_strings:
 try:
 total += int(s)
 except ValueError:
 pass  # Not a number, ignore it.


Why would you want to log that? It's not an error, it is working as
designed. I hate software that logs every little thing that happens, so
that you cannot tell what's important and what isn't.




Is perfectly right to use try catch for a flow control.
Just think in something more complex like this.

   try:
self._conn = MySQLdb.connect(host=host,
user=user,
passwd=passwd,
db=db)
except:
logging.info("Error de conexion con la base de datos")
inform(subject = 'Db down on app %s' % app, body=sbody)

Or maybe something like this.

try:
cursor.execute(sqli, data)
self._conn.commit()
except:
try:
self._conn.rollback()
cursor.execute(sqli, data)
self._conn.commit()
except Exception, e:
pass
# print e
# logging.info('ERROR en la insercion %s' % e)

This is pretty dumb, but is a valid example, on what you can do with try 
catch

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


Re: Confused compare function :)

2012-12-06 Thread Chris Angelico
On Thu, Dec 6, 2012 at 10:47 PM, Steven D'Aprano
 wrote:
> Not so. Which one is faster will depend on how often you expect to fail.
> If the keys are nearly always present, then:
>
> try:
> do_stuff(mydict[k])
> except KeyError:
> pass
>
> will be faster. Setting up a try block is very fast, about as fast as
> "pass", and faster than "if k in mydict".
>
> But if the key is often missing, then catching the exception will be
> slow, and the "if k in mydict" version may be faster. It depends on how
> often the key is missing.
>

Setting up the try/except is a constant time cost, while the
duplicated search for k inside the dictionary might depend on various
other factors. In the specific case of a Python dictionary, the
membership check is fairly cheap (assuming you're not the subject of a
hash collision attack - Py3.3 makes that a safe assumption), but if
you were about to execute a program and wanted to first find out if it
existed, that extra check could be ridiculously expensive, eg if the
path takes you on a network drive - or, worse, on multiple network
drives, which I have had occasion to do!

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Neil Cerutti
On 2012-12-05, Vlastimil Brom  wrote:
> ... PARSNIP, certified organic

I'm not sure on this one.

> ('PARSNIP', ', certified organic')

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


Re: Confused compare function :)

2012-12-06 Thread Hans Mulder
On 6/12/12 12:55:16, peter wrote:
> Is perfectly right to use try catch for a flow control.
> Just think in something more complex like this.
> 
>try:
> self._conn = MySQLdb.connect(host=host,
> user=user,
> passwd=passwd,
> db=db)
> except:
> logging.info("Error de conexion con la base de datos")
> inform(subject = 'Db down on app %s' % app, body=sbody)

This is an example of the sort of incorrect code you
should try to avoid.  An improved version is:

   self._conn = MySQLdb.connect(host=host,
user=user,
passwd=passwd,
db=db)

By not catching the exception, you're allowing the
Python interpreter to report what the problem was,
for example "Keyboard interrupt" or "Access denied".

By report "DB down" when there is no reason to assume
that that is the problem, you're confusing the user.

> Or maybe something like this.
> 
> try:
> cursor.execute(sqli, data)
> self._conn.commit()
> except:
> try:
> self._conn.rollback()
> cursor.execute(sqli, data)
> self._conn.commit()
> except Exception, e:
> pass
> # print e
> # logging.info('ERROR en la insercion %s' % e)

This is another example of what not to do.  Even the
commented-out print statement loses information, viz.
the traceback.

If you leave out the try/except, then more accurate
information will be printed, and a programmer who needs
to fix the problem, can run the code under the debugger
and it will automatically stop at the point where the
uncaught exception is raised.  That's much easier than
having to set breakpoints at all the "except Exception:"
clauses in a typical chunk of hard-to-maintain code.

Context managers were invented to make it easier to do
this sort of thing correctly.  For example:

with sqlite3.connect(dbpath) as connection:
connection.cursor().execute(sqli, data)

If the flow reaches the end of the "with" command,
the connection object will self.commit() automatically.
If an exception is raised, the connection object will
self.rollback() automatically.  No try/except required.

This is shorter, and much easier to get right.

> This is pretty dumb, but is a valid example, on what you can
> do with try catch

It is an unfortunate fact of life that you can write code
that is hard to maintain.  The fact that you *can* do this,
does not mean that you should.


Hope this helps,

-- HansM

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


Re: Confused compare function :)

2012-12-06 Thread Thomas Rachel

Am 06.12.2012 09:49 schrieb Bruno Dupuis:


The point is Exceptions are made for error handling, not for normal
workflow. I hate when i read that for example:

 try:
 do_stuff(mydict[k])
 except KeyError:
 pass


I as well, but for other reasons (see below). But basically this is EAFP.



(loads of them in many libraries and frameworks)
instead of:

 if k in mydict:
 do_stuff(mydict[k])


This is LBYL, C-style, not Pythonic.

I would do

try:
value = mydict[k]
except KeyError:
pass
else:
do_stuff(k)

Why? Because do_stuff() might raise a KeyError, which should not go 
undetected.



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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Alexander Blinne
Am 05.12.2012 18:04, schrieb Nick Mellor:
> Sample data

Well let's see what

def split_product(p):
p = p.strip()
w = p.split(" ")
try:
j = next(i for i,v in enumerate(w) if v.upper() != v)
except StopIteration:
return p, ''
return " ".join(w[:j]), " ".join(w[j:])

(which i still find a very elegant solution) has to say about those
sample data:

>>> for line in open('test.dat', 'r'):
... print(split_product(line))
('BEANS', 'hand picked')
('BEETROOT', 'certified organic')
('BOK CHOY', '(bunch)')
('BROCCOLI', 'Mornington Peninsula')
('BRUSSEL  SPROUTS', '')
('CABBAGE', 'green')
('CABBAGE', 'Red')
('CAPSICUM RED', '')
('CARROTS', '')
('CARROTS', 'loose')
('CARROTS', 'juicing, certified organic')
('CARROTS', 'Trentham, large seconds, certified organic')
('CARROTS', 'Trentham, firsts, certified organic')
('CAULIFLOWER', '')
('CELERY', 'Mornington Peninsula IPM grower')
('CELERY', 'Mornington Peninsula IPM grower')
('CUCUMBER', '')
('EGGPLANT', '')
('FENNEL', '')
('GARLIC', '(from Argentina)')
('GINGER', 'fresh uncured')
('KALE', '(bunch)')
('KOHL RABI', 'certified organic')
('LEEKS', '')
('LETTUCE', 'iceberg')
('MUSHROOM', 'cup or flat')
('MUSHROOM', 'Swiss brown')
('ONION', 'brown')
('ONION', 'red')
('ONION', 'spring (bunch)')
('PARSNIP,', 'certified organic')
('POTATOES', 'certified organic')
('POTATOES', 'Sebago')
('POTATOES', 'Desiree')
('POTATOES', 'Bullarto chemical free')
('POTATOES', 'Dutch Cream')
('POTATOES', 'Nicola')
('POTATOES', 'Pontiac')
('POTATOES', 'Otway Red')
('POTATOES', 'teardrop')
('PUMPKIN', 'certified organic')
('SCHALLOTS', 'brown')
('SNOW PEAS', '')
('SPINACH', "I'll try to get certified organic (bunch)")
('SWEET POTATO', 'gold certified organic')
('SWEET POTATO', 'red small')
('SWEDE', 'certified organic')
('TOMATOES ', 'Qld')
('TURMERIC', 'fresh certified organic')
('ZUCCHINI', '')
('APPLES', 'Harcourt  Pink Lady, Fuji, Granny Smith')
('APPLES', 'Harcourt 2 kg bags, Pink Lady or Fuji (bag)')
('AVOCADOS', '')
('AVOCADOS', 'certified organic, seconds')
('BANANAS', 'Qld, organic')
('GRAPEFRUIT', '')
('GRAPES', 'crimson seedless')
('KIWI FRUIT', 'Qld certified organic')
('LEMONS', '')
('LIMES', '')
('MANDARINS', '')
('ORANGES', 'Navel')
('PEARS', 'Beurre Bosc Harcourt new season')
('PEARS', 'Packham, Harcourt new season')
('SULTANAS', '350g pre-packed bags')
('EGGS', "Melita free range, Barker's Creek")
('BASIL', '(bunch)')
('CORIANDER', '(bunch)')
('DILL', '(bunch)')
('MINT', '(bunch)')
('PARSLEY', '(bunch)')
('', 'Spring ONION from QLD')

I think the only thing one is left to think about is the
('PARSNIP,', 'certified organic')
case. What about that extra comma? Perhaps it could even be considered
an "error" in the original data? I don't see a good general way to deal
with those which does not have to handle trailing punctuation on the
product name explicitly as a special case.

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


Re: Confused compare function :)

2012-12-06 Thread Chris Angelico
On Fri, Dec 7, 2012 at 12:32 AM, Hans Mulder  wrote:
> On 6/12/12 12:55:16, peter wrote:
>> Is perfectly right to use try catch for a flow control.
>> Just think in something more complex like this.
>>
>>try:
>> self._conn = MySQLdb.connect(host=host,
>> user=user,
>> passwd=passwd,
>> db=db)
>> except:
>> logging.info("Error de conexion con la base de datos")
>> inform(subject = 'Db down on app %s' % app, body=sbody)
>
> This is an example of the sort of incorrect code you
> should try to avoid.  An improved version is:
>
>self._conn = MySQLdb.connect(host=host,
> user=user,
> passwd=passwd,
> db=db)
>
> By not catching the exception, you're allowing the
> Python interpreter to report what the problem was,
> for example "Keyboard interrupt" or "Access denied".
>
> By report "DB down" when there is no reason to assume
> that that is the problem, you're confusing the user.

The problem with the original example is that it has a bare except,
which will catch too much. Call it an oversimplified example, perhaps
:) By not catching the exception, you doom your entire script to
abort. What Steven called felicide is catching exceptions *and
immediately exiting*, which offers little benefit over just letting
the exception propagate up.

>> Or maybe something like this.
>>
>> try:
>> cursor.execute(sqli, data)
>> self._conn.commit()
>> except:
>> try:
>> self._conn.rollback()
>> cursor.execute(sqli, data)
>> self._conn.commit()
>> except Exception, e:
>> pass
>> # print e
>> # logging.info('ERROR en la insercion %s' % e)
>
> This is another example of what not to do.  Even the
> commented-out print statement loses information, viz.
> the traceback.
>
> If you leave out the try/except, then more accurate
> information will be printed, and a programmer who needs
> to fix the problem, can run the code under the debugger
> and it will automatically stop at the point where the
> uncaught exception is raised.  That's much easier than
> having to set breakpoints at all the "except Exception:"
> clauses in a typical chunk of hard-to-maintain code.

Again, oversimplified example. If this were real code, I'd criticize
the bare except (and the "except Exception", which has the same
problem).

> Context managers were invented to make it easier to do
> this sort of thing correctly.  For example:
>
> with sqlite3.connect(dbpath) as connection:
> connection.cursor().execute(sqli, data)
>
> If the flow reaches the end of the "with" command,
> the connection object will self.commit() automatically.
> If an exception is raised, the connection object will
> self.rollback() automatically.  No try/except required.
>
> This is shorter, and much easier to get right.

But it does something completely different. The original code's logic
is: Try the query, then commit. If that fails, roll back and have
another shot at it. This is dangerous if there could have been other
statements in the transaction (they won't be retried), but otherwise,
it's a reasonable way of dealing with serialization failures. It has
its risks, of course, but it's not meant to be a demo of database
code, it's a demo of try/except.

>> This is pretty dumb, but is a valid example, on what you can
>> do with try catch
>
> It is an unfortunate fact of life that you can write code
> that is hard to maintain.  The fact that you *can* do this,
> does not mean that you should.

Agreed. However, the mere presence of try/except does not make code
unmaintainable, nor is it a strong indication that the code already
was.

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


Re: problem with usbtmc-communication

2012-12-06 Thread Jean Dubois
On 5 dec, 23:21, w...@mac.com wrote:
> On Dec 5, 2012, at 3:38 PM, Jean Dubois  wrote:
>
> [byte]
>
>
>
> >> I note that in your Octave example you are reading characters rather than 
> >> lines.  It seems to me that you have two choices here, either do the 
> >> equivalent in python or dig through the Keithley documentation to find the 
> >> hex codes thatusbtmcis presumably translating your commands into.  If you 
> >> can find those, and if you are interested, I can send you off-line the 
> >> handler I wrote a couple of years ago that used a dictionary to translate 
> >> English commands into hex, then assembled those with a checksum and sent 
> >> the string out to a Keyspan usb to serial converter.
>
> > If you could show me how to "do the equivalent in Python" I'd
> > appreciate that very much
>
> > best regards,
> > jean
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> OK - I've excerpted some of the relevant code (the original was much longer 
> and included a lot of error checking).  Please understand that the comments 
> were for my own use, this was never intended to see the light of day (as it 
> were).  Also, the structure grew from a situation where I only had to worry 
> about a single controller model - single relay model to one where I had to be 
> able to deal with two controllers and two relay models.  This was all python 
> 2.7 of course.
>
> cut on dotted line..
> """
>  serial_port = /dev/tty.KeySerial1, 2, 3, etc.
>  X10_controller = 1132B  or TI103
>  Relay_model = UM506  or RBn04
>  Relay_address = B2
>
> """
> import serial, string
>
> def checksum256(st):
>     temp = reduce(lambda x,y:x+y, map(ord,st)) % 256
>     if temp < 9:
>         hex_temp = '0'+str(temp)
>         return hex_temp
>     hex_temp = hex(temp).upper()[2:]
>     return hex_temp
>
> letr_address_dict = {'A':'\x46', 'B':'\x4E', 'C':'\x42', 'D':'\x4A', 
> 'E':'\x41', 'F':'\x49', 'G':'\x45', 'H':'\x4D', 'I':'\x47',  'J':'\x4F',  
> 'K':'\x43',  'L':'\x4B',  'M':'\x40',  'N':'\x48',  'O':'\x44',  'P':'\x4C' }
> numb_address_dict = {'1':'\x4C', '2':'\x5C', '3':'\x44', '4':'\x54', 
> '5':'\x42', '6':'\x52', '7':'\x4A', '8':'\x5A', '9':'\x4E', '10':'\x5E', 
> '11':'\x46', '12':'\x56', '13':'\x40', '14':'\x50', '15':'\x48', '16':'\x58' }
> cmd_dict     =      {'SoC':'\x63', 'All_Units_Off':'\x41', 
> 'All_Lights_On':'\x43', 'ON':'\x45', 'OFF':'\x47', 'Dim':'\x49', 
> 'Bright':'\x4B', 'All_Lights_Off':'\x4D', 'Rep_Cnt1':'\x41', 
> 'Rep_Cnt2':'\x42'}
>
> def relay(port, controller_model, relay_model, relay_address, command):
>     if controller_model == '1132B':
>         if relay_model == 'UM506' or relay_model == 'UM7206':
>             letr = letr_address_dict[relay_address[0]]
>             numb = numb_address_dict[relay_address[1]]
>             cmd = cmd_dict[command]
>             cmd_string = '\x63'+letr+numb+cmd+'\x42'     # Start-of-Command + 
> address_letter + address_number + command + Rep-count
>             ser = serial.Serial(port, 9600, timeout=1)   # Set up handle to 
> serial port
>             stat1 = ser.write('\x02')                    # Write attention to 
> PowerLink, stat = number of bytes written, not really an error return.
>             ack1 = ser.read(2)                           # Check to see if 
> PowerLink is ready
>             if ack1 == '\x06\r':                         # It returns ACK 
> (\x06\r) if it is
>                 stat2 = ser.write(cmd_string)
>                 ack2 = ser.read(19)
>                 if command == 'ON' and ack2 == 'XN\\1\rXNE1\rXNE1\r' : status 
> = 0
>                 if command == 'OFF' and ack2 == 'XN\\1\rXNG1\rXNG1\r': status 
> = 0
>                 if command == 'ON' and ack2 != 'XN\\1\rXNE1\rXNE1\r' : status 
> = 1
>                 if command == 'OFF' and ack2 != 'XN\\1\rXNG1\rXNG1\r': status 
> = 1
>             elif ack1 =='\x15':                           # PowerLink sends 
> NAC (hex 15) if it isn't.
>                 print('Received NAK from X10 Control, is there too much X10 
> traffic on the line?\n')
>             else:   print("Something's wrong with X10 control. Ack returned 
> was: " + ack1 + "\n")
>             stat3 = ser.close()                          # Close serial port
>             return(status)
>
> -
> some irrelevant stuff was here, snipped
> -
>     elif controller_model == 'TI103':
>         if relay_model == 'UM506' or relay_model == 'UM7206':
>             letr = relay_address[0]
>             numb = relay_address[1]
>             if int(relay_address[1]) <= 9:   numb = '0'+numb
>
> #           stat1 = ser.write('$>28001B02B02 BONBONCC#') # Tell TI103 to send 
> "On" to device B2
>
>             ser = serial.Serial(port, 9600, timeout=0.1)   # Set up handle to 
> serial port
>
>             cmd_string = '$>28001'+letr+numb+letr+numb+' 
> '+letr+command+letr+command
>             ck_sum = checksum256(cmd_string)
>             cmd_string = cmd_stri

Re: Confused compare function :)

2012-12-06 Thread Neil Cerutti
On 2012-12-06, Steven D'Aprano
 wrote:
> total = 0
> for s in list_of_strings:
> try:
> total += int(s)
> except ValueError:
> pass  # Not a number, ignore it.

If it's internal data, perhaps. Of course, that would mean I had
the option of *not* creating that stupid list_of_strings.

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


Re: Confused compare function :)

2012-12-06 Thread Chris Angelico
On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel

wrote:
> Am 06.12.2012 09:49 schrieb Bruno Dupuis:
>
>> The point is Exceptions are made for error handling, not for normal
>> workflow. I hate when i read that for example:
>>
>>  try:
>>  do_stuff(mydict[k])
>>  except KeyError:
>>  pass
>
> I would do
>
> try:
> value = mydict[k]
> except KeyError:
> pass
> else:
> do_stuff(k)
>
> Why? Because do_stuff() might raise a KeyError, which should not go
> undetected.

(Assuming first off that you meant "do_stuff(value)", not
"do_stuff(k)", in that last line)

That has quite different functionality, though. The original wouldn't
have called do_stuff at all if k is not in dict, behaviour which is
matched by both his EAFP and his LBLY. But your version, in the event
of a KeyError, will call do_stuff with the previous value of value, or
raise NameError if there is no such previous value. I don't think
that's intentional.

The only way around it that I can see is an extra condition or jump -
something like:

def call_if_present(mydict,k,do_stuff):
"""Equivalent to
do_stuff(mydict[k])
if the key is present; otherwise, does not call do_stuff, and
returns None."""
try:
value = mydict[k]
except KeyError:
return
return do_stuff(value)

It'll propagate any other exceptions from the subscripting (eg
TypeError if you give it a list instead of a dict), and any exceptions
from do_stuff itself. But it's getting a bit unwieldy.

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


Re: problem with usbtmc-communication

2012-12-06 Thread Jean Dubois
On 4 dec, 20:55, Terry Reedy  wrote:
> On 12/4/2012 7:14 AM, Jean Dubois wrote:
>
>
>
>
>
>
>
>
>
> > The following test program which tries to communicate with a Keithley
> > 2200 programmable power supply usingusbtmcin Python does not work as
> > expected. I have connected a 10 ohm resistor to its terminals and I
> > apply 0.025A, 0.050A, 0.075A en 0.1A,
> > I then measure the current and the voltage en write them in a file
> > De data produced looks like this:
> > 0.00544643 0.254061; first current value is wrong, voltage value is
> > correct
> > 0.0250807 0.509289; second current value is wrong, but it corresponds
> > to the first, second voltage is correct
> > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to
> > the second, 3rd voltage is right
> > 0.075099 1.01792; 4th current value is wrong,  it corresponds to the
> > 3rd, 4th voltage is right
> >                              4th correct current value is missing
>
> > But is should be (numerical inaccuracy taking into account)  (these data
> > were produced by a similar octave-program):
> > 0.0248947 0.254047
> > 0.0499105 0.509258
> > 0.0749044 0.764001
> > 0.0998926 1.01828
>
> > Here is the python-program:
> > #!/usr/bin/python
> > import time
> > import os
> > import sys
> > measurementcurr=''
> > measurementvolt=''
> > timesleepdefault=1
> > filename ='mydata.txt'
> > usbkeith = open('/dev/usbtmc1','r+')
> > usbkeith.flush()
> > usbkeith.write("*IDN?\n")
> > #strip blank line:
> > identification=usbkeith.readline().strip()
> > print 'Found device: ',identification
> > usbkeith.write("SYST:REM" + "\n")
> > usbkeith.write(":SENS:VOLT:PROT 1.5\n")
> > keithdata = open(filename,'w')
> > #start first measurement
> > usbkeith.write(":SOUR:CURR 0.025\n")
> > usbkeith.write(":OUTP:STAT ON\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write(":MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 1: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 1: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
>
> [3 near repetitions snipped]
>
> This sort of repetitious code without even line breaks is painful for me
> to read. Python has looping statements for a reason. Replace all four
> nearly identical blocks with the following. (If you are not familiar
> with built-in enumerate, you should be. Read its entry in the library
> manual.)
>
> for number, current_in in enumerate(
>      ('0.025', '0.050'. '0.075', '0.100'), 1)
>    usbkeith.write(":SOUR:CURR %s\n" % current_in)
>    ...
>    print 'Measured current %d: ' % number, measurementcurr
>    ...
>    print 'Measured voltage %d: ' % number, measurementvolt
>
> Now you can make changes in only one place and easily add more test values.
>
> > print "Goodbye, data logged in file:"
> > print filename
> > usbkeith.close()
> > keithdata.close()
>
> > can anyone here what is going wrong and how to get it right?
>
> No, but if both the python and octave programs used loops, it would be
> easier to see if both are doing the same things before, within, and
> after the loop.
>
> --
> Terry Jan Reedy

Thank you for you reply. Of course you are right this kind of code is
not the normal way to program but as I mentioned in my initial post
this is merely a test program, I did avoid using a loop on purpose
because timing can be critical in this kind of application and I
didn't want to add unnecessary uncertainty in the timing which could
be caused by using a loop.
The final version will be more like you suggested here above.
As Bill stated in another e-mail the real trouble here is some kind of
"buffer-problem",  according to him it could be caused by the use of
the readline()-statements. So if anyone could tell me how to
substitute those by some Python commands which read characters instead
does the Octave-code I'd appreciate that very much.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Vlastimil Brom
2012/12/6 Neil Cerutti :
> On 2012-12-05, Vlastimil Brom  wrote:
>> ... PARSNIP, certified organic
>
> I'm not sure on this one.
>
>> ('PARSNIP', ', certified organic')
>
> --
> Neil Cerutti
> --

Well, I wasn't either, when I noticed this item, but given the specification:
"2. Retain punctuation and parentheses"
in one of the previous OP's messages, I figured, the punctuation would
better be a part of the description rather than the name in this case.

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


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
Guys I'm still confusing my script is working better, but not enough.
I did a logfile to see which products are not found anymore in the CSV
and I found some that are present but python says they are not ??

Here is the product in the CSV:
MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;

Here is what python reports:
e2208hds-b2, 721 not found
e2273hds-b1, 722 not found
e2274hds-b2, 723 not found
e2407hds-b1, 724 not found

And here is my final code: ( I hope it look better now :) )

def Change_price(): # Changes the price in the DB if the price in the
CSV is changed
TotalUpdated = 0 # Counter for total updated
TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV
TotalSKUinDB = 0 # Total SKU in the DB
for row in PRODUCTSDB:
TotalSKUinDB +=1
db_sku = row["sku"].lower()
db_price = float(row["price"])
found = False
try:
for x in pricelist:
try:
csv_price = x[6]
csv_price = csv_price.replace(",",".")
csv_price = float(csv_price)
csv_new_price = csv_price*1.10
csv_sku = x[4].lower()
csv_stock = int(x[7]) # I used this as normally I
used  stock in the condition
match = re.search(db_sku, csv_sku)
if len(db_sku) != 0 and match:
TotalSKUFound +=1
if csv_new_price < db_price and csv_stock > 0:
print db_sku, csv_price, db_price, csv_new_price
Update_SQL(csv_new_price, db_sku)
TotalUpdated += 1
found = True

except IndexError: # I have a lot of index error in
the CSV (empty fields) and the loop gives "index error" I  don't care
about them
pass
except ValueError:
pass
except TypeError:
pass
except IndexError:
pass
if not found: WriteLog(db_sku, db_sku,)
TotalNotFound = TotalSKUinDB - TotalSKUFound
print "Total SKU in the DB %s" % TotalSKUinDB
print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound
print "Total updated: %s" % TotalUpdated
print"Total not found with in the distributor: %s" % TotalNotFound
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread Hans Mulder
On 6/12/12 14:58:01, Chris Angelico wrote:
> On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel
> 
> wrote:
>> > Am 06.12.2012 09:49 schrieb Bruno Dupuis:
>> >
>>> >> The point is Exceptions are made for error handling, not for normal
>>> >> workflow. I hate when i read that for example:
>>> >>
>>> >>  try:
>>> >>  do_stuff(mydict[k])
>>> >>  except KeyError:
>>> >>  pass
>> >
>> > I would do
>> >
>> > try:
>> > value = mydict[k]
>> > except KeyError:
>> > pass
>> > else:
>> > do_stuff(k)
>> >
>> > Why? Because do_stuff() might raise a KeyError, which should not go
>> > undetected.

> (Assuming first off that you meant "do_stuff(value)", not
> "do_stuff(k)", in that last line)

> That has quite different functionality, though. The original wouldn't
> have called do_stuff at all if k is not in dict, behaviour which is
> matched by both his EAFP and his LBLY. But your version, in the event
> of a KeyError, will call do_stuff with the previous value of value, or
> raise NameError if there is no such previous value. I don't think
> that's intentional.

Errhm, no.  Look again.  The do_stuff(value) call is in the "else:"
clause, so it will only be done of there was no Exception of any
kind, and in that case the assignment to value must have succeeded.

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


Re: Some help in refining this regex for CSV files

2012-12-06 Thread Tim Chase
On 12/06/12 01:21, Oltmans wrote:
> Hi guys,
> 
> I've to deal with CSVs that look like following
> 
> CSV (with one header and 3 legit rows where each legit row has 3 columns)
> 
> Some info
> Date: 12/6/2012
> Author: Some guy
> Total records: 100
> 
> header1, header2, header3
> one, two, three
> one, "Python is great, so are other languages, isn't ?", three
> one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n 
> this \t\t\t fact. \t\t\t\tthis fact alone is amazing'
> 
> 
> So inside this CSV, there will always be bad lines like the top 4 (they could 
> end up in the beginning, in the middle and even in the last). So above 
> sample, csv has 3 legit lines and a header. I want to read those three lines 
> and here is a regex that I came up with (which clearly isn't working)
> 
> #print line
> pattern = r"([^\t]+\t|,+)"
> matches = re.match(pattern, line) 
> 
> Do you've any better ideas guys? I will really appreciate all help.

I agree with Mark that using the "csv" module will likely be your
easiest way to go.  Just consume the lines you don't want before
passing it to the csv.reader(), or parse them and discard invalid
items.  The first could be done something like

  import csv
  f = file("data.csv", "rb")
  while True:
  line = f.next().rstrip("\r\n")
  if not line: break
  r = csv.reader(f)
  for row in r:
  print repr(row)

The latter might be done something like

  f = file("data.csv", "rb")
  r = csv.reader(f)
  for row in r:
  if len(row) != 3: continue
  print repr(row)

However, I also noticed that your example file doesn't seem to fit a
true csv file definition, as you seem to switch quoting notations,
sometimes using single, sometimes using double quotes.

-tkc



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


Re: Confused compare function :)

2012-12-06 Thread Chris Angelico
On Fri, Dec 7, 2012 at 1:21 AM, Hans Mulder  wrote:
> Errhm, no.  Look again.  The do_stuff(value) call is in the "else:"
> clause, so it will only be done of there was no Exception of any
> kind, and in that case the assignment to value must have succeeded.

DOH! My bad. Sorry! I somehow didn't see your else, *and* didn't think
of that clause when doing up my "fixed" version. Yep, else is
precisely what this calls for, and (assuming the change I mentioned)
your code does indeed match the original.

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


Re: Confused compare function :)

2012-12-06 Thread Dave Angel
On 12/06/2012 08:58 AM, Chris Angelico wrote:
> On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel
> 
> wrote:
>> Am 06.12.2012 09:49 schrieb Bruno Dupuis:
>>
>>> The point is Exceptions are made for error handling, not for normal
>>> workflow. I hate when i read that for example:
>>>
>>>  try:
>>>  do_stuff(mydict[k])
>>>  except KeyError:
>>>  pass
>> I would do
>>
>> try:
>> value = mydict[k]
>> except KeyError:
>> pass
>> else:
>> do_stuff(k)
>>
>> Why? Because do_stuff() might raise a KeyError, which should not go
>> undetected.
> (Assuming first off that you meant "do_stuff(value)", not
> "do_stuff(k)", in that last line)
>
> That has quite different functionality, though. The original wouldn't
> have called do_stuff at all if k is not in dict, behaviour which is
> matched by both his EAFP and his LBLY. But your version, in the event
> of a KeyError, will call do_stuff with the previous value of value, or
> raise NameError if there is no such previous value.

Nope.  The else clause will only execute if no exception occurs in the
value= line.

>  I don't think
> that's intentional.
>
> The only way around it that I can see is an extra condition or jump -
> something like:
>
> def call_if_present(mydict,k,do_stuff):
> """Equivalent to
> do_stuff(mydict[k])
> if the key is present; otherwise, does not call do_stuff, and
> returns None."""
> try:
> value = mydict[k]
> except KeyError:
> return
> return do_stuff(value)
>
> It'll propagate any other exceptions from the subscripting (eg
> TypeError if you give it a list instead of a dict), and any exceptions
> from do_stuff itself. But it's getting a bit unwieldy.
>
> ChrisA


-- 

DaveA

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


Re: problem with usbtmc-communication

2012-12-06 Thread wrw
On Dec 6, 2012, at 8:50 AM, Jean Dubois  wrote:

[byte]

> 
> It seems there is some misunderstanding here. What I meant with  how
> to "do the equivalent in Python" refered to  "reading characters
> rather than lines".
> I have written working code myself for another Keithleu which does use
> RS232 for communication. The problem now is specifically for the new
> Keithley which doesn't allow RS232 but only USB-communication over
> usbtmc. So if the "buffer-problem" could be changed by reading
> characters that would be great.
> 
> regards,
> Jean
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Sorry about the misunderstanding (and subsequent waste of bandwidth).  However, 
if you will look at the serial reads and writes in that handler, you will see 
that it does things like "serial.read(n)" where "n" is an explicit number, the 
number of bytes to be read from the serial buffer.

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


Re: Confused compare function :)

2012-12-06 Thread peter

On 12/06/2012 10:58 AM, Chris Angelico wrote:

On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel

wrote:

Am 06.12.2012 09:49 schrieb Bruno Dupuis:


The point is Exceptions are made for error handling, not for normal
workflow. I hate when i read that for example:

  try:
  do_stuff(mydict[k])
  except KeyError:
  pass

I would do

 try:
 value = mydict[k]
 except KeyError:
 pass
 else:
 do_stuff(k)

Why? Because do_stuff() might raise a KeyError, which should not go
undetected.

(Assuming first off that you meant "do_stuff(value)", not
"do_stuff(k)", in that last line)

That has quite different functionality, though. The original wouldn't
have called do_stuff at all if k is not in dict, behaviour which is
matched by both his EAFP and his LBLY. But your version, in the event
of a KeyError, will call do_stuff with the previous value of value, or
raise NameError if there is no such previous value. I don't think
that's intentional.

The only way around it that I can see is an extra condition or jump -
something like:

def call_if_present(mydict,k,do_stuff):
 """Equivalent to
 do_stuff(mydict[k])
 if the key is present; otherwise, does not call do_stuff, and
returns None."""
 try:
 value = mydict[k]
 except KeyError:
 return
 return do_stuff(value)

It'll propagate any other exceptions from the subscripting (eg
TypeError if you give it a list instead of a dict), and any exceptions
from do_stuff itself. But it's getting a bit unwieldy.

ChrisA
Ok, is seems like my example code, don't like :). Is ok, it was a poor 
example.

This a more complex example that create a python daemons process.
In these case you see the help of try catch. To watch an Operating 
system problem (80% of the cases), when the fork is created. Or  you can 
initialize a logging (in the catch statement)

and watch the log file with tail -f.


import sys, os
def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
# Perform first fork.
try:
pid = os.fork( )
if pid > 0:
sys.exit(0) # Exit first parent.
except OSError, e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, 
e.strerror))

sys.exit(1)
# Decouple from parent environment.
os.chdir("/")
os.umask(0)
os.setsid( )
# Perform second fork.
try:
pid = os.fork( )
if pid > 0:
sys.exit(0) # Exit second parent.
except OSError, e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, 
e.strerror))

sys.exit(1)
# The process is now daemonized, redirect standard file descriptors.
for f in sys.stdout, sys.stderr: f.flush( )
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
os.dup2(si.fileno( ), sys.stdin.fileno( ))
os.dup2(so.fileno( ), sys.stdout.fileno( ))
os.dup2(se.fileno( ), sys.stderr.fileno( ))

Or imagine that you are manage master-slave's connections (for examples 
to an ldap databse - or a monitor system tools), you can use try catch.
If all the server's are down, maybe you want to be inform about it, you 
can put a function to send a email to your account


In simple works try: catch is 'GOOD'.

ipserver = {"192.168.1.13": ["monitoreo", 22],
"192.168.1.18": ["usuarios-dns", 22, 53, 139, 389, 445, 
631, 3306, 4900, 8765],
"192.168.1.72": ["sistemas-ldap", 22, 80, 139, 389, 445, 
631, 3306, 4900, 8765],

"192.168.1.74": ["terminales", 22,139, 445, 389, 4900],
"192.168.1.80": ["backup", 22, 139, 445],
"192.168.1.1": ["router", 21, 22, 25, 80, 110, 143, 465, 3128],
"192.168.1.90": ["router", 5900]
}

def portstatus(self, **kwargs):
ports = kwargs.get("ports")
server = kwargs.get("server")
if len(ports) > 0:
logging.info("chequeando puertos %s" % server)
for a in ports:
try:
sock = socket()
sock.connect((server,a))
sock.close
except:
logging.info("informando errores en puerto %s" % a)
today = str(datetime.today())
subprocess.Popen("for a in $(seq 1 15); do beep; 
done", shell=True)
self.sendreport(ip=server, server=server, 
cuerpo="El puerto  %s esta caido en %s - %s" % (a, server, today), 
asunto="El puerto  %s esta caido en %s - %s" % (a, server, today))

else:
logging.info("no hay puertos que chequear")







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


The curses module and licensing

2012-12-06 Thread Benjamin Schnitzler
Hello everyone,

I am new to this list.
I have a question concerning the curses module (I am using
python2.7). Which license does this module have? Is it bases
completely on the curses library? In the documentation the
following is stated:

"The curses module provides an interface to the curses library,
the de-facto standard for portable advanced terminal handling."

But also:

"Changed in version 1.6: Added support for the ncurses library
and converted to a package."

Is it maybe built on both libraries? However, the crucial point
is, that I want to build a curses terminal interface for some GPL
software project. Can You give me a hint, if that is possible
(yes, I know, You are no lawyers) and if and where I have to
include licensing informations regarding the license of
curses/ncurses ?

Thank You

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


Re: mini browser with python

2012-12-06 Thread inq1ltd
On Thursday, December 06, 2012 01:52:15 AM Hans Mulder 
wrote:
> On 5/12/12 22:44:21, inq1ltd wrote:
> > I can connect to and download a web page,
> > html code, and save it to a file. If connected
> > to the web, I can change the settings on KWrite
> > to open the file and navigate the page,
> > (just like a browser does).
> > 
> > I want to view the html file without using a browser
> > 
> > or KWrite as I do now.
> > 
> > Customer wants a direct connect to a dedicated
> > website. Their clients can be using any
> > browser. This gives them a a direct connect to
> > a dedicated website for a specific purpose.
> > In other words I need a mini, simple browser;
> > something I can build that will open, read and
> > display a saved html or the connected url site.
> 
> How about:
> 
> import os.path, webbrowser
> 
> webbrowser.open("file://" + os.path.abspath(your_file))
> 
> > I would appreciate some direction.
> 
> You'd get more useful answers if you'd give some
> more context.  For example:
> 
> * Which platform?
> * Which version of Python?
> * Which GUI (if any)?
> * What capabilities do you need?  CSS? Javascript?
> * Are there any unusual requirement?
> 

A few responders have ask for this information. 

Platform can be linux or Windows.
Currently: Linux runs Python 2.7
 and Windows runs 2.6.3; 
Windows can be XP and above.
My program is built with Python and Tkinter
I convert the program using py2exe.

There are no special requirements. 
The client will design around my capabilities.

The html page will simply be a display of data. 

Right now I need some way to display 
15 to 20 lines of html in its own window or 
as part of my screen. 

I am trying to avoid using any commercial
browser. 

GTK has a webkit, and QT has a webkit and 
Suse, wich I use has libwebkitgtk-1_0-0.

All at some time I will have to look at all. 
I thought If someone had done this already 
The learning curve could be lowered.

Thanks for the interest,
jimonlinux

   


> 
> Hope this helps,
> 
> -- HansM-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Dict comprehension help

2012-12-06 Thread Joseph L. Casale
>You could put the loop into a helper function, but if you are looping
>through the same my_list more than once why not build a lookup table
>
>my_dict = {d["key"]: d for d in my_list}
>
>and then find the required dict with
>
>my_dict[value]

I suppose, what I failed to clarify was that for each list of dicts, I may
only extract out one (maybe two) dicts, but I have several to do this for.

The tidy one-liners make for populating the vars in __init__ nice and clean.

Much appreciated guys, thanks!
jlc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with usbtmc-communication

2012-12-06 Thread Jean Dubois
On 4 dec, 20:55, Terry Reedy  wrote:
> On 12/4/2012 7:14 AM, Jean Dubois wrote:
>
>
>
>
>
>
>
>
>
> > The following test program which tries to communicate with a Keithley
> > 2200 programmable power supply using usbtmc in Python does not work as
> > expected. I have connected a 10 ohm resistor to its terminals and I
> > apply 0.025A, 0.050A, 0.075A en 0.1A,
> > I then measure the current and the voltage en write them in a file
> > De data produced looks like this:
> > 0.00544643 0.254061; first current value is wrong, voltage value is
> > correct
> > 0.0250807 0.509289; second current value is wrong, but it corresponds
> > to the first, second voltage is correct
> > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to
> > the second, 3rd voltage is right
> > 0.075099 1.01792; 4th current value is wrong,  it corresponds to the
> > 3rd, 4th voltage is right
> >                              4th correct current value is missing
>
> > But is should be (numerical inaccuracy taking into account)  (these data
> > were produced by a similar octave-program):
> > 0.0248947 0.254047
> > 0.0499105 0.509258
> > 0.0749044 0.764001
> > 0.0998926 1.01828
>
> > Here is the python-program:
> > #!/usr/bin/python
> > import time
> > import os
> > import sys
> > measurementcurr=''
> > measurementvolt=''
> > timesleepdefault=1
> > filename ='mydata.txt'
> > usbkeith = open('/dev/usbtmc1','r+')
> > usbkeith.flush()
> > usbkeith.write("*IDN?\n")
> > #strip blank line:
> > identification=usbkeith.readline().strip()
> > print 'Found device: ',identification
> > usbkeith.write("SYST:REM" + "\n")
> > usbkeith.write(":SENS:VOLT:PROT 1.5\n")
> > keithdata = open(filename,'w')
> > #start first measurement
> > usbkeith.write(":SOUR:CURR 0.025\n")
> > usbkeith.write(":OUTP:STAT ON\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write(":MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 1: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 1: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
>
> [3 near repetitions snipped]
>
> This sort of repetitious code without even line breaks is painful for me
> to read. Python has looping statements for a reason. Replace all four
> nearly identical blocks with the following. (If you are not familiar
> with built-in enumerate, you should be. Read its entry in the library
> manual.)
>
> for number, current_in in enumerate(
>      ('0.025', '0.050'. '0.075', '0.100'), 1)
>    usbkeith.write(":SOUR:CURR %s\n" % current_in)
>    ...
>    print 'Measured current %d: ' % number, measurementcurr
>    ...
>    print 'Measured voltage %d: ' % number, measurementvolt
>
> Now you can make changes in only one place and easily add more test values.
>
> > print "Goodbye, data logged in file:"
> > print filename
> > usbkeith.close()
> > keithdata.close()
>
> > can anyone here what is going wrong and how to get it right?
>
> No, but if both the python and octave programs used loops, it would be
> easier to see if both are doing the same things before, within, and
> after the loop.
>
> --
> Terry Jan Reedy
I followed your suggestion an now the code looks like this:
#!/usr/bin/python
import time
import os
import sys
measurementcurr=''
measurementvolt=''
timesleepdefault=2
filename ='mydata.txt'
usbkeith = open('/dev/usbtmc1','r+')
usbkeith.flush()
usbkeith.write("*IDN?\n")
#strip blank line:
identification=usbkeith.readline().strip()
print 'Found device: ',identification
usbkeith.write("SYST:REM" + "\n")
usbkeith.write(":SENS:VOLT:PROT 1.5\n")
keithdata = open(filename,'w')
usbkeith.write(":OUTP:STAT ON\n")
for number, current_in in enumerate(('0.025', '0.050', '0.075',
'0.100'), 1):
   usbkeith.write(":SOUR:CURR %s\n" % current_in)
   time.sleep(timesleepdefault)
   usbkeith.write(":MEAS:CURR?\n")
   measurementcurr=usbkeith.readline()
   print 'Measured current %d: ' % number, measurementcurr
   usbkeith.write(":MEAS:VOLT?\n")
   measurementvolt=usbkeith.readline()
   print 'Measured voltage %d: ' % number, measurementvolt
   keithdata.write(measurementcurr.strip()+' '+measurementvolt)
usbkeith.write(":OUTP:STAT OFF\n")
print "Goodbye, data logged in file:"
print filename
usbkeith.close()
keithdata.close()

Still there is a "buffer-problem" as you can see in the output below:
0.00639725 0.0104065; these values are completely wrong
0.0248976 0.262959; these should have been be the first values
0.0500431 0.516602: these should been the second values
0.0749168 0.772616; these are the 3rd values
 4th values are missing

any idea why this does what it does in Python?

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


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
On Thu, Dec 6, 2012 at 3:22 PM, Anatoli Hristov  wrote:
> Guys I'm still confusing my script is working better, but not enough.
> I did a logfile to see which products are not found anymore in the CSV
> and I found some that are present but python says they are not ??
>
> Here is the product in the CSV:
> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;
>
> Here is what python reports:
> e2208hds-b2, 721 not found
> e2273hds-b1, 722 not found
> e2274hds-b2, 723 not found
> e2407hds-b1, 724 not found
>
> And here is my final code: ( I hope it look better now :) )
>
> def Change_price(): # Changes the price in the DB if the price in the
> CSV is changed
> TotalUpdated = 0 # Counter for total updated
> TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV
> TotalSKUinDB = 0 # Total SKU in the DB
> for row in PRODUCTSDB:
> TotalSKUinDB +=1
> db_sku = row["sku"].lower()
> db_price = float(row["price"])
> found = False
> try:
> for x in pricelist:
> try:
> csv_price = x[6]
> csv_price = csv_price.replace(",",".")
> csv_price = float(csv_price)
> csv_new_price = csv_price*1.10
> csv_sku = x[4].lower()
> csv_stock = int(x[7]) # I used this as normally I
> used  stock in the condition
> match = re.search(db_sku, csv_sku)
> if len(db_sku) != 0 and match:
> TotalSKUFound +=1
> if csv_new_price < db_price and csv_stock > 0:
> print db_sku, csv_price, db_price, csv_new_price
> Update_SQL(csv_new_price, db_sku)
> TotalUpdated += 1
> found = True
>
> except IndexError: # I have a lot of index error in
> the CSV (empty fields) and the loop gives "index error" I  don't care
> about them
> pass
> except ValueError:
> pass
> except TypeError:
> pass
> except IndexError:
> pass
> if not found: WriteLog(db_sku, db_sku,)
> TotalNotFound = TotalSKUinDB - TotalSKUFound
> print "Total SKU in the DB %s" % TotalSKUinDB
> print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound
> print "Total updated: %s" % TotalUpdated
> print"Total not found with in the distributor: %s" % TotalNotFound

No one have an idea ?

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


How to list a file which already created a 2 mins ago

2012-12-06 Thread moonhkt
Hi All

AIX.5.3
Python 2.6.2

File ftp to Machine A, need to rename then send to Machine B.

How to list a file which already created a 2 mins ago ?  If file aging
more than 2 mins. I want to rename file to other file name.

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


Re: Confused compare function :)

2012-12-06 Thread Mark Lawrence

On 06/12/2012 16:16, Anatoli Hristov wrote:



No one have an idea ?

Thanks



Basically because your code is crap.  Others have already suggested 
refactoring your code to make it easier to follow.  Try (diabolical pun 
very deliberate) following that advice.  Failing that find out how much 
it'll cost to fix and get your cheque book out, perhaps that'll focus 
your mind.  I'll not apologise for being blunt as your follow up is less 
than two hours after your previous post.


--
Cheers.

Mark Lawrence.

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


Re: question about importing a package

2012-12-06 Thread Matt
It works now. Steven and Alex, thanks for your help!

I ended up leaving sample.py and foo.py and bar.p the way they were, and in 
__init__.py putting:

from foo import *
from bar import *

So my mistake was not importing the foo and bar modules into 
sub_one/__init__.py.

I also see how the __all__ array helps me control what gets imported. I can 
leave it out of __init__.py, and everything gets imported. So my three lessons 
are:

1) "from X import *" will look for an __all__ list in module X, or in 
__init__.py if X is a package instead of a module, and import only what is in 
that list. Module names are different than function names in that list.
2) if __all__ is not defined, "from X import *' will import everything in X's 
namespace
3) __init__.py acts like just another module, so you have to import the package 
contents that you want into it before you import the package into your code

Thanks again for the help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New tutorials

2012-12-06 Thread Mitya Sirenef

On 12/02/2012 09:54 AM, Mitya Sirenef wrote:

Hi everyone, I'm making a  series of python tutorials and I've just

> finished the introductory part: http://lightbird.net/larks/intro.html
>
> Feedback is appreciated.. - mitya
> 


I've added two more tutorials, Sudoku and Battleship games:

http://lightbird.net/larks/sudoku.html

http://lightbird.net/larks/battleship.html


As with Blocky Blocks, I'm not sure unicode chars in Battleship will
work well for everyone on all OSes, I've only tested it on Linux
X terminal.   -m

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


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
>> No one have an idea ?
>>
>> Thanks
>>
>
> Basically because your code is crap.  Others have already suggested
> refactoring your code to make it easier to follow.

Thank you Mark for your notes. I changed the name of the variables as
was suggested before.
I know my code should be crappy, but at least I try as I'm a beginner
in a programming at all - this is a good exercise for me.

>Try (diabolical pun very
> deliberate) following that advice.  Failing that find out how much it'll
> cost to fix and get your cheque book out, perhaps that'll focus your mind.
> I'll not apologise for being blunt as your follow up is less than two hours
> after your previous post.

It seems the arrogance is in your blood :p Nice shot


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


Re: Confused compare function :)

2012-12-06 Thread MRAB

On 2012-12-06 14:22, Anatoli Hristov wrote:

Guys I'm still confusing my script is working better, but not enough.
I did a logfile to see which products are not found anymore in the CSV
and I found some that are present but python says they are not ??

Here is the product in the CSV:
MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;

Here is what python reports:
e2208hds-b2, 721 not found
e2273hds-b1, 722 not found
e2274hds-b2, 723 not found
e2407hds-b1, 724 not found

And here is my final code: ( I hope it look better now :) )

def Change_price(): # Changes the price in the DB if the price in the CSV is 
changed
 TotalUpdated = 0 # Counter for total updated
 TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV
 TotalSKUinDB = 0 # Total SKU in the DB
 for row in PRODUCTSDB:
 TotalSKUinDB +=1
 db_sku = row["sku"].lower()
 db_price = float(row["price"])
 found = False
 try:
 for x in pricelist:
 try:
 csv_price = x[6]
 csv_price = csv_price.replace(",",".")
 csv_price = float(csv_price)
 csv_new_price = csv_price*1.10
 csv_sku = x[4].lower()
 csv_stock = int(x[7]) # I used this as normally I used  
stock in the condition

>  match = re.search(db_sku, csv_sku)

This line is equivalent to:

match = db_sku in csv_sku

This means that it's looking for db_sku anywhere in csv_sku. For
example, all of these are true: "72" in "720"; "20" in "720"; "720" in
"720"; etc.


 if len(db_sku) != 0 and match:
 TotalSKUFound +=1


TotalSKUFound is incremented if it finds a match.

Can "db_sku in csv_sku" be true multiple times for a given value of
db_sku? (See above.) Is it possible that TotalSKUFound is incremented
multiple times for some values of csv_sku?


 if csv_new_price < db_price and csv_stock > 0:
 print db_sku, csv_price, db_price, csv_new_price
 Update_SQL(csv_new_price, db_sku)
 TotalUpdated += 1
 found = True


It sets found to True if it updated.


 except IndexError: # I have a lot of index error in the CSV (empty 
fields) and the loop gives "index error" I  don't care about them
 pass
 except ValueError:
 pass
 except TypeError:
 pass


Even after finding a match (and possibly updating), it continues
iterating though pricelist.


 except IndexError:


There's no need to catch IndexError here because the only places it
could be raised are also within the inner try..except.


 pass
 if not found: WriteLog(db_sku, db_sku,)


Calling it 'found' is misleading, because it's True only if it updated.
If it found a match but didn't update, 'found' will still be False.


 TotalNotFound = TotalSKUinDB - TotalSKUFound
 print "Total SKU in the DB %s" % TotalSKUinDB
 print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound
 print "Total updated: %s" % TotalUpdated
 print"Total not found with in the distributor: %s" % TotalNotFound


Using a loop within a loop like this could be the cause of your
problem. It's certainly not the most efficient way of doing it.

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


Re: New tutorials

2012-12-06 Thread Jean-Michel Pichavant
- Original Message -
> On 12/02/2012 09:54 AM, Mitya Sirenef wrote:
> > Hi everyone, I'm making a  series of python tutorials and I've just
>  > finished the introductory part:
>  > http://lightbird.net/larks/intro.html
>  >
>  > Feedback is appreciated.. - mitya
>  > 
> 
> 
> I've added two more tutorials, Sudoku and Battleship games:
> 
> http://lightbird.net/larks/sudoku.html
> 
> http://lightbird.net/larks/battleship.html
> 
> 
> As with Blocky Blocks, I'm not sure unicode chars in Battleship will
> work well for everyone on all OSes, I've only tested it on Linux
> X terminal.   -m
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

You should summarize the new python concept introduced by each tutorial so 
people can jump directly to the section they need.

I have the feeling this is not really a python tutorial, it's a bunch of mini 
games that happen to be written in python. Am I right ? If I'm wrong, you 
should definitely organize your doc by python concepts, linking each concept to 
any related tutorial.

JM


 


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mini browser with python

2012-12-06 Thread John Gordon
In  inq1ltd 
 writes:

> Right now I need some way to display 
> 15 to 20 lines of html in its own window or 
> as part of my screen. 

Could you open a shell window and run a text web browser such as Lynx?

-- 
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: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
>> Here is the product in the CSV:
>> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
>> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;

This one is still not found and it is in the CSV file - I just don't
get it why !

>>  if len(db_sku) != 0 and match:
>>  TotalSKUFound +=1
> TotalSKUFound is incremented if it finds a match.
>
> Can "db_sku in csv_sku" be true multiple times for a given value of
> db_sku? (See above.) Is it possible that TotalSKUFound is incremented
> multiple times for some values of csv_sku?

Most of the SKU's are not doubled in the CSV, but what I will do is to
create new function to compare each SKU so after it founds the SKU in
the file it will return right away to the DB loop.

>>  if csv_new_price < db_price and csv_stock > 0:
>>  print db_sku, csv_price, db_price,
>> csv_new_price
>>  Update_SQL(csv_new_price, db_sku)
>>  TotalUpdated += 1
>>  found = True

> It sets found to True if it updated.
>>  except IndexError: # I have a lot of index error in the
>> CSV (empty fields) and the loop gives "index error" I  don't care about them
>>  pass
>>  except ValueError:
>>  pass
>>  except TypeError:
>>  pass
>
>
> Even after finding a match (and possibly updating), it continues
> iterating though pricelist.

Yes it will change after I create the new func.as I assume.

>>  except IndexError:
>
>
 if not found: WriteLog(db_sku, db_sku,)
>
>
> Calling it 'found' is misleading, because it's True only if it updated.
> If it found a match but didn't update, 'found' will still be False.
> Using a loop within a loop like this could be the cause of your
> problem. It's certainly not the most efficient way of doing it.

I will keep you posted THANK YOU
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread MRAB

On 2012-12-06 17:31, Anatoli Hristov wrote:

Here is the product in the CSV:
MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;


This one is still not found and it is in the CSV file - I just don't
get it why !


[snip]
It's not saying that it's not found, it's saying that it wasn't updated 
because:


csv_new_price < db_price and csv_stock > 0

was False. Is the new price higher or the same?
--
http://mail.python.org/mailman/listinfo/python-list


FitsStubImagePlugin

2012-12-06 Thread Colm Brazel
Hi

Anybody got links to, eg how to use, FitsStubImagePlugin module part of PIL?

thanks

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


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov

 Here is the product in the CSV:
 MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide
 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;
> [snip]
> It's not saying that it's not found, it's saying that it wasn't updated
> because:

You are right, the price at the distributor is higher than the one I
have :) I will update the price what ever the price is I will not
compare it anymore.

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


PyCharm messed up my PyDev

2012-12-06 Thread Jabba Laci
Hi,

I have a strange problem. I've used PyDev for a long time and a few
days ago I wanted to try PyCharm. When I wanted to create a new
project in PyCharm, it asked me to select the interpreter. There was a
possibility to upgrade packages so I selected all and pressed the
upgrade button. For the upgrade, it asked my admin password (I'm under
Linux).
Then today I wanted to go back to PyDev but it says "Python not
configured" for _all_ my projects :( Under Preferences I can select
the interpreter (Auto Config) but when I click on OK, the selection is
not held. When I go back to the interpreter selection window, it's
empty again. Clicking on OK it says "Restoring PYTHONPATH".

I think it's because of PyCharm. I did the same thing on my laptop and
I have the same problem. Has anyone met this issue? How to get back a
working PyDev?

I also tried Aptana Studio 3 after Eclipse, but the same thing
happens. Did PyCharm change some permissions that PyDev doesn't like?

Thanks,

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


Re: The curses module and licensing

2012-12-06 Thread benjamin schnitzler
I think I have accidentially not sent this to the python list:

On 02:17 Fri 07 Dec , Chris Angelico wrote:
> Hi!
>
> Here's some info on ncurses:
>
> http://invisible-island.net/ncurses/ncurses.faq.html
>
> I would generally assume that it's safe to use any Python standard
> library module without worrying about what license its underlying
> library/ies are distributed under - because I trust the Python core
> developers to ensure that they're creating a product that's not bound
> by unexpected license terms. But you'll probably get an on-list
> response with more detail.
>
> Chris Angelico
Thanks for your answer. I guess you're right. It's just, that I
never really worked for a project where I got into license
issues. (At least I think so.) And I am not really used to it.
The FAQ states:

"Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, distribute with
modifications, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Except as contained in this notice, the name(s) of the above
copyright holders shall not be used in advertising or otherwise
to promote the sale, use or other dealings in this Software
without prior written authorization."

"...to deal in the Software without restriction..."
"...distribute, distribute with modifications, sublicense..."

Sounds good. Ok, but I have to include this into all "copies or
substantial portions of the Software" . Ok, thats a bit vague.
And if I include this notice into my files: It sounds a bit, as
if this would be then a copyright for them.
Substantial portions are, I guess, the libraries themselves.
Which are other substantial portions? Those files which extend
the libraries maybe? I looked into the files of my
/usr/lib/python2.7/curses directory. None of them had this text
portion from above included. On the other hand the FAQ also
states that the Free Software Foundation, who got hold of the
copyright, had promised to use the above distribution terms only
for 5 years, which was in 1998, so it might not be relevant at
all. Well ok. I guess I can just leave my files, as they are,
since they are no real extensions of the library. But I am not
completely sure. If anyone has a better idea: Just let me know
it.

Benjamin


On Thu, Dec 6, 2012 at 4:02 PM, Benjamin Schnitzler <
benjaminschnitz...@googlemail.com> wrote:

> Hello everyone,
>
> I am new to this list.
> I have a question concerning the curses module (I am using
> python2.7). Which license does this module have? Is it bases
> completely on the curses library? In the documentation the
> following is stated:
>
> "The curses module provides an interface to the curses library,
> the de-facto standard for portable advanced terminal handling."
>
> But also:
>
> "Changed in version 1.6: Added support for the ncurses library
> and converted to a package."
>
> Is it maybe built on both libraries? However, the crucial point
> is, that I want to build a curses terminal interface for some GPL
> software project. Can You give me a hint, if that is possible
> (yes, I know, You are no lawyers) and if and where I have to
> include licensing informations regarding the license of
> curses/ncurses ?
>
> Thank You
>
> Benjamin
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mini browser with python

2012-12-06 Thread inq1ltd
On Thursday, December 06, 2012 05:19:38 PM John Gordon 
wrote:
> In  
inq1ltd  writes:
> > Right now I need some way to display
> > 15 to 20 lines of html in its own window or
> > as part of my screen.
> 
> Could you open a shell window and run a text web browser 
such as Lynx?

That is a possibility but then every machine
will have to have Lynx.  That is not quite
what the customer wants.
  
Also, I can download the site to a file, convert 
the data to text,  then display it in a Text widget. 

I appreciate the help,
Regards,
jimonlinux
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread Anatoli Hristov
> gmane.comp.python.general:
>
>> But basically, the code seems to run a pair of nested for-loops:
>>
>> for SKU in database:
>> for SKU in csv file:
>> if the two SKUs match:
>> compare their prices and update the database
>>
> OUCH...
>
> I'm presuming the CSV is restarted each time the database record
> changes...
>
> That would seem better reformulated as:
>
> for (SKU, price) in CSV:
> DB.execute("update SKUtable set price = %s where SKU = %s",
> (price, SKU))
> --
I don't know if I get it right, but
Nope, each loop I'm getting:
for x in CSV:
MONIIE2409HDS-B1;MON;II;E2409HDS-B1;E2409HDS-B1;IIYAMA LCD 24" Wide
1920x1080 TN Panel Speakers 2MS Black;130;9;RECTD0.41;0,41;

So x[4] is my SKU and x[5] is price and so on. Each loop looks like this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCharm messed up my PyDev

2012-12-06 Thread Jabba Laci
Dear All,

I managed to solve the problem. Something got messed up in the
.metadata directory. After renaming it, Aptana Studio re-created it
and I could successfully specify the Python interpreter.

In PyCharm I specified the same workspace to use, maybe that was the problem.

Laszlo

On Thu, Dec 6, 2012 at 7:30 PM, Jabba Laci  wrote:
> Hi,
>
> I have a strange problem. I've used PyDev for a long time and a few
> days ago I wanted to try PyCharm. When I wanted to create a new
> project in PyCharm, it asked me to select the interpreter. There was a
> possibility to upgrade packages so I selected all and pressed the
> upgrade button. For the upgrade, it asked my admin password (I'm under
> Linux).
> Then today I wanted to go back to PyDev but it says "Python not
> configured" for _all_ my projects :( Under Preferences I can select
> the interpreter (Auto Config) but when I click on OK, the selection is
> not held. When I go back to the interpreter selection window, it's
> empty again. Clicking on OK it says "Restoring PYTHONPATH".
>
> I think it's because of PyCharm. I did the same thing on my laptop and
> I have the same problem. Has anyone met this issue? How to get back a
> working PyDev?
>
> I also tried Aptana Studio 3 after Eclipse, but the same thing
> happens. Did PyCharm change some permissions that PyDev doesn't like?
>
> Thanks,
>
> Laszlo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread Rotwang

On 06/12/2012 08:49, Bruno Dupuis wrote:

On Thu, Dec 06, 2012 at 04:32:34AM +, Steven D'Aprano wrote:

On Thu, 06 Dec 2012 03:22:53 +, Rotwang wrote:


On 06/12/2012 00:19, Bruno Dupuis wrote:

[...]

Another advice: never ever

except XXXError:
  pass

at least log, or count, or warn, or anything, but don't pass.


Really? I've used that kind of thing several times in my code. For
example, there's a point where I have a list of strings and I want to
create a list of those ints that are represented in string form in my
list, so I do this:

listofints = []
for k in listofstrings:
try:
listofints.append(int(k))
except ValueError:
pass

Another example: I have a dialog box with an entry field where the user
can specify a colour by entering a string, and a preview box showing the
colour. I want the preview to automatically update when the user has
finished entering a valid colour string, so whenever the entry field is
modified I call this:

def preview(*args):
try:
previewbox.config(bg = str(entryfield.get()))
except tk.TclError:
pass

Is there a problem with either of the above? If so, what should I do
instead?


They're fine.

Never, ever say that people should never, ever do something.


*cough*



Well, dependening on the context (who provides listofstrings?) I would
log or count errors on the first one... or not.


The actual reason for the first example is that I have a text widget 
with a bunch of tags (which are identified by strings), and I want to 
add a new tag whose name doesn't coincide with any of the existing tag 
names. I achieve this by setting my listofstrings equal to the list of 
existing tag names, and setting the new tag name as


str(max(listofints) + 1) if listofints else '0'

I realise that there are a bunch of other ways I could have done this. 
But I haven't a clue how I could rewrite the second example without 
using a try statement (other than by writing a function that would 
recognise when a string defines a valid Tkinter colour, including the 
long and possibly version-dependent list of colours with Zoolanderesque 
names like 'LightSteelBlue3').




On the second one, I would split the expression, because (not sure of
that point, i didn't import tk for years) previewbox.config and
entryfield.get may raise a tk.TclError for different reasons.

The point is Exceptions are made for error handling, not for normal
workflow.


Although I'm something of a noob, I'm pretty sure the Python community 
at large would disagree with this, as evidenced by the fact that 'EAFP' 
is an entry in the official Python glossary:


EAFP
Easier to ask for forgiveness than permission. This common Python
coding style assumes the existence of valid keys or attributes and
catches exceptions if the assumption proves false. This clean and
fast style is characterized by the presence of many try and except
statements. The technique contrasts with the LBYL style common to
many other languages such as C.

(from http://docs.python.org/2/glossary.html)

--
I have made a thing that superficially resembles music:

http://soundcloud.com/eroneity/we-berated-our-own-crapiness
--
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-06 Thread Rotwang

On 06/12/2012 04:32, Steven D'Aprano wrote:

On Thu, 06 Dec 2012 03:22:53 +, Rotwang wrote:

[...]

Is there a problem with either of the above? If so, what should I do
instead?


They're fine.

Never, ever say that people should never, ever do something.


*cough*


Thanks.


--
I have made a thing that superficially resembles music:

http://soundcloud.com/eroneity/we-berated-our-own-crapiness
--
http://mail.python.org/mailman/listinfo/python-list


Re: The curses module and licensing

2012-12-06 Thread Alister
On Thu, 06 Dec 2012 19:34:04 +0100, benjamin schnitzler wrote:

> I think I have accidentially not sent this to the python list:
> 
> On 02:17 Fri 07 Dec , Chris Angelico wrote:
>> Hi!
>>
>> Here's some info on ncurses:
>>
>> http://invisible-island.net/ncurses/ncurses.faq.html
>>
>> I would generally assume that it's safe to use any Python standard
>> library module without worrying about what license its underlying
>> library/ies are distributed under - because I trust the Python core
>> developers to ensure that they're creating a product that's not bound
>> by unexpected license terms. But you'll probably get an on-list
>> response with more detail.
>>
>> Chris Angelico
> Thanks for your answer. I guess you're right. It's just, that I never
> really worked for a project where I got into license issues. (At least I
> think so.) And I am not really used to it.
> The FAQ states:
> 
> "Permission is hereby granted, free of charge, to any person obtaining a
> copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use,
> copy, modify, merge, publish, distribute, distribute with modifications,
> sublicense, and/or sell copies of the Software,
> and to permit persons to whom the Software is furnished to do so,
> subject to the following conditions:
> The above copyright notice and this permission notice shall be included
> in all copies or substantial portions of the Software.
> 
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
> DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
> OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
> THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> 
> Except as contained in this notice, the name(s) of the above copyright
> holders shall not be used in advertising or otherwise to promote the
> sale, use or other dealings in this Software without prior written
> authorization."
> 
> "...to deal in the Software without restriction..."
> "...distribute, distribute with modifications, sublicense..."
> 
> Sounds good. Ok, but I have to include this into all "copies or
> substantial portions of the Software" . Ok, thats a bit vague.
> And if I include this notice into my files: It sounds a bit, as if this
> would be then a copyright for them.
> Substantial portions are, I guess, the libraries themselves.
> Which are other substantial portions? Those files which extend the
> libraries maybe? I looked into the files of my /usr/lib/python2.7/curses
> directory. None of them had this text portion from above included. On
> the other hand the FAQ also states that the Free Software Foundation,
> who got hold of the copyright, had promised to use the above
> distribution terms only for 5 years, which was in 1998, so it might not
> be relevant at all. Well ok. I guess I can just leave my files, as they
> are,
> since they are no real extensions of the library. But I am not
> completely sure. If anyone has a better idea: Just let me know it.
> 
> Benjamin
> 
> 
> On Thu, Dec 6, 2012 at 4:02 PM, Benjamin Schnitzler <
> benjaminschnitz...@googlemail.com> wrote:
> 
>> Hello everyone,
>>
>> I am new to this list.
>> I have a question concerning the curses module (I am using python2.7).
>> Which license does this module have? Is it bases completely on the
>> curses library? In the documentation the following is stated:
>>
>> "The curses module provides an interface to the curses library,
>> the de-facto standard for portable advanced terminal handling."
>>
>> But also:
>>
>> "Changed in version 1.6: Added support for the ncurses library and
>> converted to a package."
>>
>> Is it maybe built on both libraries? However, the crucial point is,
>> that I want to build a curses terminal interface for some GPL software
>> project. Can You give me a hint, if that is possible (yes, I know, You
>> are no lawyers) and if and where I have to include licensing
>> informations regarding the license of curses/ncurses ?
>>
>> Thank You
>>
>> Benjamin
>>
> I think I have accidentially not sent this to the python
> list:On 02:17 Fri 07 Dec     , Chris Angelico
> wrote:> Hi!> > Here's some
> info on ncurses:
> > >  href="http://invisible-island.net/ncurses/ncurses.faq.html";>http://
invisible-island.net/ncurses/ncurses.faq.html> >
> I would generally assume that it's safe to use any Python
> standard
> > library module without worrying about what license its
> underlying> library/ies are distributed under - because I
> trust the Python core> developers to ensure that
> they're creating a product that's not bound
> > by unexpected license terms. But you'll probably get an
> on-list> response with more
> detail.> > Chris AngelicoThanks
> for y

Re: problem with usbtmc-communication

2012-12-06 Thread Jean Dubois
On 6 dec, 15:50, w...@mac.com wrote:
> On Dec 6, 2012, at 8:50 AM, Jean Dubois  wrote:
>
> [byte]
>
>
>
>
>
>
>
>
>
>
>
> > It seems there is some misunderstanding here. What I meant with  how
> > to "do the equivalent in Python" refered to  "reading characters
> > rather than lines".
> > I have written working code myself for another Keithleu which does use
> > RS232 for communication. The problem now is specifically for the new
> > Keithley which doesn't allow RS232 but only USB-communication over
> > usbtmc. So if the "buffer-problem" could be changed by reading
> > characters that would be great.
>
> > regards,
> > Jean
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> Sorry about the misunderstanding (and subsequent waste of bandwidth).  
> However, if you will look at the serial reads and writes in that handler, you 
> will see that it does things like "serial.read(n)" where "n" is an explicit 
> number, the number of bytes to be read from the serial buffer.
>
> -Bill
I tried changing measurementcurr=usbkeith.readline() to
measurementcurr=usbkeith.read(1)
but this leads to trouble with the usbtmc-thing:

Measured current 1:
Traceback (most recent call last):
  File "./keith2200rev2.py", line 26, in 
measurementvolt=usbkeith.read(1)
IOError: [Errno 110] Connection timed out

and hereafter I need to restart the Keithley...:-(

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


Re: problem with usbtmc-communication

2012-12-06 Thread Dave Angel
On 12/06/2012 02:41 PM, Jean Dubois wrote:
> On 6 dec, 15:50, w...@mac.com wrote:
>> 
>> Sorry about the misunderstanding (and subsequent waste of bandwidth).  
>> However, if you will look at the serial reads and writes in that handler, 
>> you will see that it does things like "serial.read(n)" where "n" is an 
>> explicit number, the number of bytes to be read from the serial buffer.
>>
>> -Bill
> I tried changing measurementcurr=usbkeith.readline() to
> measurementcurr=usbkeith.read(1)
> but this leads to trouble with the usbtmc-thing:
>
> Measured current 1:
> Traceback (most recent call last):
>   File "./keith2200rev2.py", line 26, in 
> measurementvolt=usbkeith.read(1)
> IOError: [Errno 110] Connection timed out
>
> and hereafter I need to restart the Keithley...:-(
I can't see why you used a count of 1.  Isn't the whole problem
supposed to be because it doesn't produce a whole line at a time?  So
after requesting a measurement, if you know the size, use that in the
read() method.  And if you don't know the size, read it one byte at a
time till it make sense.

-- 

DaveA

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


Re: problem with usbtmc-communication

2012-12-06 Thread wrw

On Dec 6, 2012, at 2:41 PM, Jean Dubois  wrote:

> On 6 dec, 15:50, w...@mac.com wrote:
>> On Dec 6, 2012, at 8:50 AM, Jean Dubois  wrote:
>> 
>> [byte]
>> 
>> 
>>> It seems there is some misunderstanding here. What I meant with  how
>>> to "do the equivalent in Python" refered to  "reading characters
>>> rather than lines".
>>> I have written working code myself for another Keithleu which does use
>>> RS232 for communication. The problem now is specifically for the new
>>> Keithley which doesn't allow RS232 but only USB-communication over
>>> usbtmc. So if the "buffer-problem" could be changed by reading
>>> characters that would be great.
>> 
>>> regards,
>>> Jean
>> 
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>> 
>> Sorry about the misunderstanding (and subsequent waste of bandwidth).  
>> However, if you will look at the serial reads and writes in that handler, 
>> you will see that it does things like "serial.read(n)" where "n" is an 
>> explicit number, the number of bytes to be read from the serial buffer.
>> 
>> -Bill
> I tried changing measurementcurr=usbkeith.readline() to
> measurementcurr=usbkeith.read(1)
> but this leads to trouble with the usbtmc-thing:
> 
> Measured current 1:
> Traceback (most recent call last):
>  File "./keith2200rev2.py", line 26, in 
>measurementvolt=usbkeith.read(1)
> IOError: [Errno 110] Connection timed out
> 
> and hereafter I need to restart the Keithley...:-(
> 
> regards,
> Jean
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Several comments:

1)  I can't be sure, but that would seem to be asking the Keithley to be 
providing 10,000 readings.  I don't know about the GPIB bus (which this USBTMC 
library seems to be trying to emulate), but most serial devices expect to 
provide one answer per read-write handshake.  That is, you send one read 
command and get one answer back.  That answer may contain several bytes, but 
I'd be surprised it it contained 10,000.  The typical cycle is something like 
send-an-initialize, read-status, send-mode-set-up, read-status, send trigger 
command, read-answer…  lather and repeat.   (Or some logical equivalent of all 
that).  On the assumption that the USBTMC API handles most of that, I'd try 
usbkeith.read(n) where "n" is the number of decimal digits you expect to get 
back plus sign.

-

2) I took a quick look at the Keithley and National Instruments web sites 
(where the documentation is at best, VERY poorly indexed and hard to search 
for).  USBTMC *appears* to be a software layer designed to allow newer 
Tektronix and Keithley instruments to be driven using older software that drove 
GPIB equipment.  To make matters worse, if I'm reading it right (I didn't study 
in detail) it appears to ALSO be providing a GPIB-like API to Windows versions 
of National Instruments LabView.

3)  If I understand what you are trying to do, you want to go straight from 
python to the Keithley USB port, without detouring (USB-to-GPIB and GPIB back 
to USB).

4)  I did find (but did not try to read in detail) the following link:  
http://www.ni.com/white-paper/4478/en  which documents direct USB control of 
instruments.  The python serial library provides quite transparent control of 
reading and writing to the USB interface.  Maybe following this link will get 
you going.

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


Re: mini browser with python

2012-12-06 Thread Terry Reedy

On 12/6/2012 1:15 PM, inq1ltd wrote:

On Thursday, December 06, 2012 05:19:38 PM John Gordon wrote:

 > In  inq1ltd
 writes:
 > > Right now I need some way to display
 > > 15 to 20 lines of html in its own window or
 > > as part of my screen.



 > Could you open a shell window and run a text web browser such as Lynx?



That is a possibility but then every machine
will have to have Lynx. That is not quite
what the customer wants.



Also, I can download the site to a file,


Or you can download and work with it in memory.

> convert the data to text,

Using existing html parser.

> then display it in a Text widget.

The complication in displaying html is with placing images and other 
boxed content, including tables, and flowing text around the boxes. If 
the html you want to work with is simple and has a somewhat fixed 
format, you should be able to extract all the info as either text or a 
table and be able to format and place it how you want.


--
Terry Jan Reedy

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


Re: problem with usbtmc-communication

2012-12-06 Thread Terry Reedy

On 12/6/2012 10:44 AM, Jean Dubois wrote:


I followed your suggestion an now the code looks like this:
#!/usr/bin/python
import time
import os
import sys
measurementcurr=''
measurementvolt=''
timesleepdefault=2
filename ='mydata.txt'
usbkeith = open('/dev/usbtmc1','r+')
usbkeith.flush()
usbkeith.write("*IDN?\n")
#strip blank line:
identification=usbkeith.readline().strip()
print 'Found device: ',identification
usbkeith.write("SYST:REM" + "\n")
usbkeith.write(":SENS:VOLT:PROT 1.5\n")
keithdata = open(filename,'w')
usbkeith.write(":OUTP:STAT ON\n")
for number, current_in in enumerate(('0.025', '0.050', '0.075',
'0.100'), 1):
usbkeith.write(":SOUR:CURR %s\n" % current_in)
time.sleep(timesleepdefault)
usbkeith.write(":MEAS:CURR?\n")
measurementcurr=usbkeith.readline()
print 'Measured current %d: ' % number, measurementcurr
usbkeith.write(":MEAS:VOLT?\n")
measurementvolt=usbkeith.readline()
print 'Measured voltage %d: ' % number, measurementvolt
keithdata.write(measurementcurr.strip()+' '+measurementvolt)
usbkeith.write(":OUTP:STAT OFF\n")
print "Goodbye, data logged in file:"
print filename
usbkeith.close()
keithdata.close()

Still there is a "buffer-problem" as you can see in the output below:
0.00639725 0.0104065; these values are completely wrong
0.0248976 0.262959; these should have been be the first values
0.0500431 0.516602: these should been the second values
0.0749168 0.772616; these are the 3rd values
  4th values are missing

any idea why this does what it does in Python?


I am not familiar with the protocol at all, but my guess (without 
looking at the octave code) is that two of these three commands


> usbkeith.write("SYST:REM" + "\n")
> usbkeith.write(":SENS:VOLT:PROT 1.5\n")
> usbkeith.write(":OUTP:STAT ON\n")

before the loop have responses that you need to read (and toss?)

usbkeith.readline(); usbkeith.readline()

so that the first values you read in the loop are the one that should be 
first. In other words, make sure the read buffer is clear before the loop.


--
Terry Jan Reedy

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


Re: question about importing a package

2012-12-06 Thread Terry Reedy

On 12/6/2012 11:50 AM, Matt wrote:

It works now. Steven and Alex, thanks for your help!

I ended up leaving sample.py and foo.py and bar.p the way they were, and in 
__init__.py putting:

from foo import *
from bar import *

So my mistake was not importing the foo and bar modules into 
sub_one/__init__.py.

I also see how the __all__ array helps me control what gets imported. I can 
leave it out of __init__.py, and everything gets imported. So my three lessons 
are:

1) "from X import *" will look for an __all__ list in module X, or in 
__init__.py if X is a package instead of a module, and import only what is in that list. 
Module names are different than function names in that list.
2) if __all__ is not defined, "from X import *' will import everything in X's 
namespace


... that does not have a leading underscore. This is why there are 
things like


import sys as _sys
from itertools import chain as _chain

in the stdlib when the module author does not define __all__.


3) __init__.py acts like just another module, so you have to import the package 
contents that you want into it before you import the package into your code



--
Terry Jan Reedy

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


Re: Secretly passing parameter to function

2012-12-06 Thread Paul Rubin
Olivier Scalbert  writes:
> # We ask the framework to do some work.
> do(step1, param = None)

from functools import partial
do(partial(step1, param = None))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Secretly passing parameter to function

2012-12-06 Thread Paul Rubin
Paul Rubin  writes:
> from functools import partial
> do(partial(step1, param = None))

Or more directly:

  do(lambda: step1(param = None))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to list a file which already created a 2 mins ago

2012-12-06 Thread Irmen de Jong
On 6-12-2012 17:49, moonhkt wrote:
> Hi All
> 
> AIX.5.3
> Python 2.6.2
> 
> File ftp to Machine A, need to rename then send to Machine B.
> 
> How to list a file which already created a 2 mins ago ?  If file aging
> more than 2 mins. I want to rename file to other file name.
> 
> moonhkt
> 

ftplib.FTP
os.path.getmtime
os.rename
time.time

Should be some pointers to get started.


Irmen

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Paul Rubin
Nick Mellor  writes:
> I came across itertools.dropwhile only today, then shortly afterwards
> found Raymond Hettinger wondering, in 2007, whether to drop [sic]
> dropwhile and takewhile from the itertools module
> Almost nobody else of the 18 respondents seemed to be using them.

What?  I'm amazed by that.  I didn't bother reading the old thread, but
I use those functions fairly frequently.  I just used takewhile the
other day, processing a timestamped log file where I wanted to look at
certain clusters of events.  I won't post the actual code here, but
takewhile was a handy way to pull out intervals of interest after an
event was seen.
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem using py-bt, py-locals, etc. during GDB debugging

2012-12-06 Thread Mark Shroyer
I'm having trouble with the py-bt, py-locals, etc. GDB commands (from
Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was
wondering if anyone here could offer advice.

Briefly, py-bt seems to identify the python stack frames correctly, but
shows "(unable to read python frame information)" for each, and likewise
py-locals fails with "Unable to read information on python frame".

The full GDB session is shown here:

https://gist.github.com/4228342#file_gdb_output.txt

My test script is as follows:

from __future__ import print_function
from os import kill, getpid
from signal import SIGINT

def factorial(n):
if n == 0:
# Break into GDB
kill(getpid(), SIGINT)
return 1
else:
return n * factorial(n-1)

if __name__ == "__main__":
n = 10
print("factorial({0}) = {1}".format(n, factorial(n)))

And I'm seeing this with both the following sets of software:

 1. Ubuntu 12.04's python-dbg 2.7.3 and GDB 7.4 packages

 2. Freshly-built copies of GDB 7.5 and Python 2.7.3 (the latter built
with -O0 -g3 in Ubuntu's GCC 4.6.3), after loading that Python's own
copy of python-gdb.py into GDB.

Any ideas?  Thanks...
Mark

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


Cosine Similarity

2012-12-06 Thread subhabangalore
Dear Group,

I am looking for some example of implementing Cosine similarity in python. I 
searched for hours but could not help much. NLTK seems to have a module but did 
not find examples. 

If anyone of the learned members may kindly help out.

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


The curses module and licensing

2012-12-06 Thread Benjamin Schnitzler
On 19:28 Thu 06 Dec , Alister wrote:
> If I understand things correctly this means if you distribute the python 
> package (alone or as part of your application) then you need to include 
> the detailed section.
> 
> if you provide just your own python code & require the user to install 
> python & any necessary modules (which would be needed for windoze users 
> python is usually installed or at least included in the repository's of 
> most Linux Distros)then this restriction does not apply.
> 
> the copyright is for the python interpreter & modules not your own 
> personal code.
Ok, thank you! I think I am clear about it now. As you might have
guessed, python will not be distributed with the package. 

By the way: Sorry for posting the statement of Chris Angelico to
the list, it wasn't meant to be on it.
> 
> 
> -- 
> I know on which side my bread is buttered.
>   -- John Heywood
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: paramiko.BadAuthenticationType

2012-12-06 Thread who
FWIW, I'm trying to write a python program that does the equivalent this perl 
program:

http://devcentral.f5.com/downloads/iControl/SDK/sslvpn.public.pl.txt

I don't see any code there that refers to a public key, but it works fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The curses module and licensing

2012-12-06 Thread Chris Angelico
On Fri, Dec 7, 2012 at 9:37 AM, Benjamin Schnitzler
 wrote:
> By the way: Sorry for posting the statement of Chris Angelico to
> the list, it wasn't meant to be on it.

Apology accepted :) It was off-list mainly because it strayed
off-topic, not because there was anything particularly private in it.
No harm done!

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


Re: New tutorials

2012-12-06 Thread Mitya Sirenef

On 12/06/2012 12:16 PM, Jean-Michel Pichavant wrote:

- Original Message -

>> On 12/02/2012 09:54 AM, Mitya Sirenef wrote:
>>> Hi everyone, I'm making a series of python tutorials and I've just
>> > finished the introductory part:
>> > http://lightbird.net/larks/intro.html
>> >
>> > Feedback is appreciated.. - mitya
>> > 
>>
>>
>> I've added two more tutorials, Sudoku and Battleship games:
>>
>> http://lightbird.net/larks/sudoku.html
>>
>> http://lightbird.net/larks/battleship.html
>>
>>
>> As with Blocky Blocks, I'm not sure unicode chars in Battleship will
>> work well for everyone on all OSes, I've only tested it on Linux
>> X terminal. -m
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> You should summarize the new python concept introduced by each 
tutorial so people can jump directly to the section they need.



That's a good idea, I was thining of something along the same lines, I
wanted to add a few more games first but I guess there's enough of them
already to make this useful. I'll try to add an 'index' section
tomorrow.




>
> I have the feeling this is not really a python tutorial, it's a bunch
> of mini games that happen to be written in python. Am I right ? If I'm
> wrong, you should definitely organize your doc by python concepts,
> linking each concept to any related tutorial.


It is meant as a python tutorial, it's just not fleshed out yet. I want
to add a few games and take a step back and figure out what order to put
them in and how to spread out detailed explanation across all sections.

Thanks for the feedback!

 -m




> JM
>
>


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


Re: Problem using py-bt, py-locals, etc. during GDB debugging [solved]

2012-12-06 Thread Mark Shroyer
On Thu, Dec 06, 2012 at 04:39:41PM -0500, Mark Shroyer wrote:
> I'm having trouble with the py-bt, py-locals, etc. GDB commands (from
> Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was
> wondering if anyone here could offer advice.
> 
> Briefly, py-bt seems to identify the python stack frames correctly, but
> shows "(unable to read python frame information)" for each, and likewise
> py-locals fails with "Unable to read information on python frame".

OK, I took a closer look at this and I've identified the issue; posting
my fix here in case someone else Googles this.

The problem in my case was that the python-gdb.py extension makes some
fragile assumptions about the format of values it receives from GDB, and
I had the line "set output-radix 16" in my .gdbinit, ultimately
resulting in the extension attempting to convert the string
representation of a hexadecimal number into an integer.

So there are two easy workarounds:

 1. set output-radix 10 in GDB, or

 2. Patch Python 2.7.3's python-gdb.py as follows:

=== 8< =

--- python-gdb.py.orig  2012-12-06 15:12:18.666760664 -0500
+++ python-gdb.py   2012-12-06 19:17:19.588356874 -0500
@@ -1074,7 +1074,11 @@
 
 
 def int_from_int(gdbval):
-return int(str(gdbval))
+int_str = str(gdbval)
+if int_str.startswith("0x"):
+return int(int_str[2:], 16)
+else:
+return int(int_str)
 
 
 def stringify(val):

=== 8< =

I haven't checked to see whether this also applies to the GDB extension
for Python 3, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem using py-bt, py-locals, etc. during GDB debugging [solved]

2012-12-06 Thread MRAB

On 2012-12-07 00:22, Mark Shroyer wrote:

On Thu, Dec 06, 2012 at 04:39:41PM -0500, Mark Shroyer wrote:

I'm having trouble with the py-bt, py-locals, etc. GDB commands (from
Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was
wondering if anyone here could offer advice.

Briefly, py-bt seems to identify the python stack frames correctly, but
shows "(unable to read python frame information)" for each, and likewise
py-locals fails with "Unable to read information on python frame".


OK, I took a closer look at this and I've identified the issue; posting
my fix here in case someone else Googles this.

The problem in my case was that the python-gdb.py extension makes some
fragile assumptions about the format of values it receives from GDB, and
I had the line "set output-radix 16" in my .gdbinit, ultimately
resulting in the extension attempting to convert the string
representation of a hexadecimal number into an integer.

So there are two easy workarounds:

  1. set output-radix 10 in GDB, or

  2. Patch Python 2.7.3's python-gdb.py as follows:

=== 8< =

--- python-gdb.py.orig  2012-12-06 15:12:18.666760664 -0500
+++ python-gdb.py   2012-12-06 19:17:19.588356874 -0500
@@ -1074,7 +1074,11 @@


  def int_from_int(gdbval):
-return int(str(gdbval))
+int_str = str(gdbval)
+if int_str.startswith("0x"):
+return int(int_str[2:], 16)
+else:
+return int(int_str)


  def stringify(val):

=== 8< =

I haven't checked to see whether this also applies to the GDB extension
for Python 3, though.


A shorter fix would be:

def int_from_int(gdbval):
return int(str(gdbval), 0)


Some examples:

>>> # Decimal
>>> int("12", 0)
12
>>> # Hexadecimal
>>> int("0x12", 0)
18
>>> # Octal
>>> int("012", 0)
10
>>> # Octal
>>> int("0o12", 0)
10
>>> # Binary
>>> int("0b11", 0)
3

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


Re: Confused compare function :)

2012-12-06 Thread Steven D'Aprano
On Thu, 06 Dec 2012 13:51:29 +, Neil Cerutti wrote:

> On 2012-12-06, Steven D'Aprano
>  wrote:
>> total = 0
>> for s in list_of_strings:
>> try:
>> total += int(s)
>> except ValueError:
>> pass  # Not a number, ignore it.
> 
> If it's internal data, perhaps. Of course, that would mean I had the
> option of *not* creating that stupid list_of_strings.


Not necessarily, it depends on the application.

If you have a spreadsheet, and create a formula =SUM(A1:A50) the expected 
behaviour is to just skip anything that is not a valid number, not raise 
an error. Sometimes correct application-level behaviour is to just ignore 
input that it doesn't care about.

One of the things that I used to despise with a passion about MYOB is 
that if you clicked on the window outside of a button or field, it would 
scream at you "ERROR ERROR ERROR - that was not a button or field" 
That is to say, it would beep. I mean, *who fscking cares* that the user 
clicks in the window background? It's a harmless tick, like tapping your 
desk, just ignore it.

As a general rule, library functions should be strict about reporting 
errors, while applications may be more forgiving about errors that they 
don't care about. The "don't care about" part is important though -- your 
word processor shouldn't care about low level networking errors, but it 
should care if it can't save a file to a network drive. 


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


Re: How to list a file which already created a 2 mins ago

2012-12-06 Thread wrw
On Dec 6, 2012, at 4:29 PM, Irmen de Jong  wrote:

> On 6-12-2012 17:49, moonhkt wrote:
>> Hi All
>> 
>> AIX.5.3
>> Python 2.6.2
>> 
>> File ftp to Machine A, need to rename then send to Machine B.
>> 
>> How to list a file which already created a 2 mins ago ?  If file aging
>> more than 2 mins. I want to rename file to other file name.
>> 
>> moonhkt
>> 
> 
> ftplib.FTP
> os.path.getmtime
> os.rename
> time.time
> 
> Should be some pointers to get started.
> 
> 
> Irmen
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

The answer will depend on whether or not the AIX "ls" command includes a switch 
to list the file creation time.  Some versions of UNIX (including OS X) have 
this, others don't.

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


Re: Cosine Similarity

2012-12-06 Thread Miki Tebeka
On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba...@gmail.com wrote:
> I am looking for some example of implementing Cosine similarity in python. I 
> searched for hours but could not help much. NLTK seems to have a module but 
> did not find examples. 
Should be easy with numpy:
import numpy as np

def cos(v1, v2):
   return np.dot(v1, v2) / (np.sqrt(np.dot(v1, v1)) * np.sqrt(np.dot(v2, 
v2)))


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


Is __ne__ method autogenerated?

2012-12-06 Thread INADA Naoki
The reference says:

  The truth of x==y does not imply that x!=y is false.
  Accordingly, when defining __eq__(), one should also
  define __ne__() so that the operators will behave as expected.

(http://docs.python.org/3/reference/datamodel.html#object.__eq__)

But I saw different behavior on 3.3:
https://gist.github.com/4231096

Could anyone teach me what happen about my code?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem using py-bt, py-locals, etc. during GDB debugging [solved]

2012-12-06 Thread Mark Shroyer
On Thu, Dec 06, 2012 at 07:37:22PM -0500, MRAB wrote:
> On 2012-12-07 00:22, Mark Shroyer wrote:
> >
> > [...]
> >
> >   2. Patch Python 2.7.3's python-gdb.py as follows:
> >
> > === 8< =
> >
> > --- python-gdb.py.orig  2012-12-06 15:12:18.666760664 -0500
> > +++ python-gdb.py   2012-12-06 19:17:19.588356874 -0500
> > @@ -1074,7 +1074,11 @@
> >
> >
> >   def int_from_int(gdbval):
> > -return int(str(gdbval))
> > +int_str = str(gdbval)
> > +if int_str.startswith("0x"):
> > +return int(int_str[2:], 16)
> > +else:
> > +return int(int_str)
> >
> >
> >   def stringify(val):
> >
> > === 8< =
> >
> > I haven't checked to see whether this also applies to the GDB extension
> > for Python 3, though.
> >
> A shorter fix would be:
> 
> def int_from_int(gdbval):
>  return int(str(gdbval), 0)
> 
> 
> Some examples:
> 
>  >>> # Decimal
>  >>> int("12", 0)
> 12
>  >>> # Hexadecimal
>  >>> int("0x12", 0)
> 18
>  >>> # Octal
>  >>> int("012", 0)
> 10
>  >>> # Octal
>  >>> int("0o12", 0)
> 10
>  >>> # Binary
>  >>> int("0b11", 0)
> 3

Nice, I didn't know about that... thanks!

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


Re: Is __ne__ method autogenerated?

2012-12-06 Thread Ian Kelly
On Thu, Dec 6, 2012 at 11:03 PM, INADA Naoki  wrote:
> The reference says:
>
>   The truth of x==y does not imply that x!=y is false.
>   Accordingly, when defining __eq__(), one should also
>   define __ne__() so that the operators will behave as expected.
>
> (http://docs.python.org/3/reference/datamodel.html#object.__eq__)
>
> But I saw different behavior on 3.3:
> https://gist.github.com/4231096
>
> Could anyone teach me what happen about my code?

http://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods

I can't find it documented anywhere else.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is __ne__ method autogenerated?

2012-12-06 Thread Chris Rebert
On Thursday, December 6, 2012, INADA Naoki wrote:

> The reference says:
>
>   The truth of x==y does not imply that x!=y is false.
>   Accordingly, when defining __eq__(), one should also
>   define __ne__() so that the operators will behave as expected.
>
> (http://docs.python.org/3/reference/datamodel.html#object.__eq__)
>
> But I saw different behavior on 3.3:
> https://gist.github.com/4231096
>
> Could anyone teach me what happen about my code?
>

The reference is not completely accurate in this case. See
 http://bugs.python.org/issue4395
"Document auto __ne__ generation; [...]"


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


Re: Is __ne__ method autogenerated?

2012-12-06 Thread INADA Naoki
Thanks a million!


On Fri, Dec 7, 2012 at 3:47 PM, Chris Rebert  wrote:

> On Thursday, December 6, 2012, INADA Naoki wrote:
>
>> The reference says:
>>
>>   The truth of x==y does not imply that x!=y is false.
>>   Accordingly, when defining __eq__(), one should also
>>   define __ne__() so that the operators will behave as expected.
>>
>> (http://docs.python.org/3/reference/datamodel.html#object.__eq__)
>>
>> But I saw different behavior on 3.3:
>> https://gist.github.com/4231096
>>
>> Could anyone teach me what happen about my code?
>>
>
> The reference is not completely accurate in this case. See
>  http://bugs.python.org/issue4395
> "Document auto __ne__ generation; [...]"
>
>
> --
> Cheers,
> Chris
> --
> http://rebertia.com
>



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


Re: Tcl wasn't installed properly

2012-12-06 Thread bhargavigoswami
I faced the same problem during my last installation of ns2.35 in ubuntu 11.04.
After I install ns2.35, got message of successful installation of ns. Then I 
set path in /.bashrc. Then I gave ns command which gave me same error which you 
got. 

The problem is because, ns executable is also at /usr which is conflicting.

Solution: 
1. Go to location root-usr-local-bin by giving following command in terminal
cd /usr/local/bin
2. There you would find the ns file. We just need to remove it by giving 
following command
rm ns
3. Thats it, you are done. Now your ns starts running successfully.

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


Re: Tcl wasn't installed properly

2012-12-06 Thread Gene Heskett
On Friday 07 December 2012 02:25:33 bhargavigosw...@gmail.com did opine:

> I faced the same problem during my last installation of ns2.35 in ubuntu
> 11.04. After I install ns2.35, got message of successful installation
> of ns. Then I set path in /.bashrc. Then I gave ns command which gave
> me same error which you got.
> 
> The problem is because, ns executable is also at /usr which is
> conflicting.
> 
Than change the order in your $PATH, or better yet, do as below, because 
the $PATH is normally ordered to find locally installed stuff first.

> Solution:
> 1. Go to location root-usr-local-bin by giving following command in
> terminal cd /usr/local/bin
> 2. There you would find the ns file. We just need to remove it by giving
> following command rm ns
> 3. Thats it, you are done. Now your ns starts running successfully.
> 
> Happy Learning.


Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
My web page:  is up!
BOFH excuse #276:

U.S. Postal Service
I was taught to respect my elders, but its getting 
harder and harder to find any...
-- 
http://mail.python.org/mailman/listinfo/python-list


pyodbc utf-8

2012-12-06 Thread Markus Christen
good morning

i am using pyodbc 3.0.6 for win32 python 2.7.3
i used it to connect with a MsSql db. Now i have a little problem with the 
umlaut. i cant change anything in the db and there are umlauts like "ä", "ö" 
and "ü" saved. so i have to change my view (of django 1.4.1) to change \xfc 
into ü etc. but how i have to do this?
on my webpage the umlauts are correct (without helping fonts like ü (ü)). 
but not the umlauts out read out of my db.

Here the code i'm using:
-
conn = pyodbc.connect('DRIVER={SQL 
Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader')
cursor = conn.cursor()
cursor.execute("SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama")
rows = cursor.fetchall()
-

helping tags like ", 'utf-8'" or something else didnt work till now. have 
anyone an idea how i can fix this problem? ^^

i thanks for help
-- 
http://mail.python.org/mailman/listinfo/python-list