MVC web-framework with RESTfully Decoupled Views?
I have used a variety of different MVC web-frameworks. My current methodology for web and mobile [PhoneGap/Cordova] development is as follows: Web framework (MVC) 1. Write the Models (db schema stuff) 2. Write the Controllers (manage interface between models and views, or just expose certain data; e.g.: RESTfully) 3. Write the Views (HTML &etc for presentation to the viewer; usually includes a seamless Template, Form and Auth layer from the framework) JavaScript framework (MVC) 1. Write the Models (for storing what the controllers grab) 2. Write the Controllers (grab required stuff RESTfully, also do sanisation type stuff [pun intended!]) 3. Write the Views >---< It seems to me that I would be much better served by a web-framework which integrates decoupled Views. This would shorten development time and prevent pseudo double-typing. Are there any Python web-frameworks which has added functionality for generating decoupled views? Thanks for all suggestions, Alec Taylor BTW: Full disclosure, also posted this on stackoverflow: http://stackoverflow.com/q/13761200 -- http://mail.python.org/mailman/listinfo/python-list
Re: pyodbc utf-8
On 7/12/12 08:41:27, Markus Christen wrote: > 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. Which encoding does your webpage use? > 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 think the way forward would be to look at the data your code snippet receives from the database. Which datatype do the strings have? Unicode or str or something else? If the type is str, which encoding do they use? If this isn't documented, you could at a few strings containing non-ascii characters to see what codes are used, and compare them to popular encodings such as uft8, latin1 and cp1252. Hope his helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
python 3.3 urllib.request
hello, I have a python3 script with urllib.request which have a strange behavior, here is the script : + #!/usr/bin/env python3 # -*- coding: utf-8 -*- import urllib.request import sys, time url = 'http://google.com' def make_some_stuff(page, url): sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") + url + "\"\n") sys.stderr.write(str(page) + "\"\n") return True def get_page(url): while 1: try: page = urllib.request.urlopen(url) yield page except urllib.error.URLError as e: sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> impossible to access to \"") + url + "\"\n") time.sleep(5) continue def main(): print('in main') for page in get_page(url): make_some_stuff(page, url) time.sleep(5) if __name__ == '__main__': main() + if the computer is connected on internet (with an ethernet connection for example) and I run this script, it works like a charme : - urllib.request.urlopen return the page - make_some_stuff write in stderr - when the ethernet cable is unplug the except block handle the error while the cable is unplug, and when the cable is pluged back urllib.request.urlopen return the page and make_some_stuff write in stderr this is the normal behavior (for me, imho). but if the computer is not connected on internet (ethernet cable unpluged) and I run this script, the except block handle the error (normal), but when I plug the cable, the script continue looping and urllib.request.urlopen never return the page (so, it alway go to the except block) What can I do to handle that ? Thanks Steeve -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused compare function :)
>> 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 And here is my final code -- I hope you will like it a little more :) def Change_price(): # Changes the price in the DB if the price in the CSV is changed TotalUpdated = 0 # Counter for total updated TotalSKUNotFound = 0 # Total SKU from the DB coresponds to the one in the CSV TotalSKUinDB = 0 found = None for row in PRODUCTSDB: TotalSKUinDB +=1 db_sku = row["sku"].lower() db_price = float(row["price"]) if CompareWithCSV(db_sku, db_price) == True: TotalUpdated +=1 else: TotalSKUNotFound +=1 Update_SQL_stock(db_sku) WriteLog(db_sku, row["product_id"]) print "Total updated: %s" % TotalUpdated print"Total not found with in the distributor: %s" % TotalSKUNotFound print "Total products in the DB %s" % TotalSKUinDB def CompareWithCSV(db_sku, db_price): 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 if len(db_sku) != 0 and db_sku == csv_sku and csv_stock > 0: print db_sku, csv_price, db_price, csv_new_price Update_SQL(csv_new_price, db_sku) return True except (IndexError, ValueError, TypeError, db_sku): # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them WriteErrorLog("Error with CSV file loop: ", db_sku) except IndexError: WriteErrorLog("Error with CSV file loop: ", db_sku) return False -- http://mail.python.org/mailman/listinfo/python-list
A question about readability
Hi all, do you think this code: $ more myscript.py for line in open('data.txt'): result = sum(int(data) for data in line.split(';')) print(result) that sums the elements of the lines of this file: $ more data.txt 30;44;99;88 11;17;16;50 33;91;77;15 $ python3.3 myscript.py 261 94 216 is explicit enough? Do you prefer a clearer solution? Thanks in advance, Marco -- Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with usbtmc-communication
On 6 dec, 21:15, w...@mac.com wrote: > 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. 1 wasn't a good guess indeed > - > > 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). > Yes indeed, that's exactly what I want > 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. Thanks for the link, but as you can see there they want to push NI- VISA forward as the solution, which under Linux means more complexity and surely isn't as simple to install as they claim, so if possible I'd avoid ni-visa. I'll experiment further Monday with read() and keep you informed regards, Jean -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with usbtmc-communication
On 6 dec, 21:28, Terry Reedy wrote: > 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?) No they don't need to have there responses be red, first command sets remote mode second command sets limit on voltage across output terminals 3rd command connects terminals with DUT > usbkeith.readline(); usbkeith.readline() This doesn't work because nothing is sent back > 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 I'll look at it further monday regards, jean -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about readability
In article , Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > result = sum(int(data) for data in line.split(';')) > print(result) That sum() line is a bit of a mouthful. I would refactor it into something like this: > for line in open('data.txt'): > fields = line.split(';') > print sum(int(i) for i in fields) It's the same number of lines, but I think it's easier to read. BTW, are you using Python 2 or 3? It helps when asking these kinds of questions to let people know. I assumed 2 in my answer above. -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about readability
On Fri, 07 Dec 2012 14:46:03 +0100, Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > result = sum(int(data) for data in line.split(';')) > print(result) [...] > is explicit enough? Do you prefer a clearer solution? Thanks in advance, It's perfectly fine for such a simple script. It is readable and explicit. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with __str__ method and character encoding
Risposta al messaggio di gialloporpora : This is the code in my test.py: Sorry, I have wrongly pasted the code: class msgmarker(object): def __init__(self, msgid, msgstr, index, encoding="utf-8"): self._encoding =encoding self.set(msgid, msgstr) self._index = index def __repr__(self): return "" def __str__(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def isUntranslated(self): return self.msgid==self.msgstr def isFuzzy(self): return self.msgstr[0:2]=="==" def markFuzzy(self): self.msgstr = "==%s" %(self.msgstr) def set(self, msgid, msgstr): if not(isinstance(msgid, unicode)): msgid = msgid.decode(self._encoding) if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgid = msgid self.msgstr = msgstr def setmsgstr(self, msgstr): if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgstr = msgstr def getIndex(self): return self._index def getIndex(self): return self._index -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant – One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with __str__ method and character encoding
On Sat, Dec 8, 2012 at 1:14 AM, gialloporpora wrote: > Dear all, > I have a problem with character encoding. > I have created my class and I have redefined the __str__ method for pretty > printing. I have saved my file as test.py, > I give these lines: > from test import * a = msgmarker("why", u"perché", 0) print a > UnicodeError print a.__str__() > OK Your __str__ method is not returning a string. It's returning a Unicode object. Under Python 2 (which you're obviously using, since you use print as a statement), strings are bytes. The best thing to do would be to move to Python 3.3, in which the default string type is Unicode, and there's a separate 'bytes' type for communicating with file systems and networks and such. But if that's not possible, I would recommend having a separate method for returning a Unicode string (the same as your current __str__ but with a different name), and have __str__ call that and encode it - something like this: def describe(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def __str__(self): return self.describe().encode(self._encoding) But it'd definitely be better to move to Python 3. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with __str__ method and character encoding
On 12/07/2012 11:17 AM, gialloporpora wrote: Risposta al messaggio di gialloporpora : This is the code in my test.py: Sorry, I have wrongly pasted the code: class msgmarker(object): def __init__(self, msgid, msgstr, index, encoding="utf-8"): self._encoding =encoding self.set(msgid, msgstr) self._index = index def __repr__(self): return "" def __str__(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def isUntranslated(self): return self.msgid==self.msgstr def isFuzzy(self): return self.msgstr[0:2]=="==" def markFuzzy(self): self.msgstr = "==%s" %(self.msgstr) def set(self, msgid, msgstr): if not(isinstance(msgid, unicode)): msgid = msgid.decode(self._encoding) if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgid = msgid self.msgstr = msgstr def setmsgstr(self, msgstr): if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgstr = msgstr def getIndex(self): return self._index def getIndex(self): return self._index what do you meant whe you say 'pretty print' ??. is you just want to print a object in a nice format, you can use pprint from pprint import pprint nasty_dict = { 'hellou': bybye, . Imagine that nasty_dict have many complex object. For pretty print in the python console you can do this: pprint(nasty_dict). Is you use ipython: %page nasty_dict -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with __str__ method and character encoding
On Sat, Dec 8, 2012 at 1:14 AM, gialloporpora wrote: print a > UnicodeError print a.__str__() > OK By the way, it's *much* more helpful to copy and paste the actual error message and output, rather than retyping like that. Spending one extra minute in the interactive interpreter before you post can save us all a lot of time and confusion. In this case, what I'm seeing is (Python 2.6): >>> print a Traceback (most recent call last): File "", line 1, in print a UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 27: ordinal not in range(128) Which has quite a bit of handy information in it. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with __str__ method and character encoding
Risposta al messaggio di Chris Angelico : Your __str__ method is not returning a string. It's returning a Unicode object. Under Python 2 (which you're obviously using, since you use print as a statement), strings are bytes. The best thing to do would be to move to Python 3.3, in which the default string type is Unicode, and there's a separate 'bytes' type for communicating with file systems and networks and such. But if that's not possible, I would recommend having a separate method for returning a Unicode string (the same as your current __str__ but with a different name), and have __str__ call that and encode it - something like this: def describe(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def __str__(self): return self.describe().encode(self._encoding) But it'd definitely be better to move to Python 3. Thanks, now I have understood. I have problem with the installer of the Python 3.3 (and 2.7.3), when I fix these problem I'll try to update to Python 3.3. Sandro -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant – One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq -- http://mail.python.org/mailman/listinfo/python-list
Python-UK Community on google+
Hi All, I created the Python-UK Community on google+, so if you are using google+ and you are interested in Python and you are interested in the UK, I heartily invite you to join. My intention is to transfer the ownership to the PyCon UK organization, which I already have contacted a member of. You can find it here: https://plus.google.com/u/0/communities/109155400666012015869 Hope to see you soon :-) Martin P. Hellwig -- 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月6日星期四UTC+8下午7时07分35秒,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,)) > > thanks,but it still doesn't 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 ? > > > > 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: pyodbc utf-8
My webpage is using UTF-8 and utf-8 or unicode is on the DB. When i read out this files with Excel, i have no problems with the umlauts. I heared its a problem of pyodbc itself, cause it is only using ascii i think. I found this page here, but it was not really helpful for me. :( maybe i have not understood what this really means... http://stackoverflow.com/questions/4805267/problem-with-unicode-decoding maybe you can understand it and you can translate it for a noob. :D Am Freitag, 7. Dezember 2012 13:16:09 UTC+1 schrieb Hans Mulder: > On 7/12/12 08:41:27, Markus Christen wrote: > > > 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. > > > > Which encoding does your webpage use? > > > > > 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 think the way forward would be to look at the data your code snippet > > receives from the database. > > > > Which datatype do the strings have? Unicode or str or something else? > > > > If the type is str, which encoding do they use? > > If this isn't documented, you could at a few strings containing > > non-ascii characters to see what codes are used, and compare > > them to popular encodings such as uft8, latin1 and cp1252. > > > > > > Hope his helps, > > > > -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: pyodbc utf-8
On 07/12/2012 15:47, Markus Christen wrote: My webpage is using UTF-8 and utf-8 or unicode is on the DB. When i read out this files with Excel, i have no problems with the umlauts. I heared its a problem of pyodbc itself, cause it is only using ascii i think. I found this page here, but it was not really helpful for me. :( maybe i have not understood what this really means... http://stackoverflow.com/questions/4805267/problem-with-unicode-decoding maybe you can understand it and you can translate it for a noob. :D Try http://www.joelonsoftware.com/articles/Unicode.html or http://nedbatchelder.com/text/unipain.html -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: pyodbc utf-8
On Fri, Dec 7, 2012 at 12:41 AM, Markus Christen wrote: > 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? ^^ What *exactly* is the output of this code? I have a hunch that it is actually outputting the correct data as a Unicode string, and you are looking at the repr of the string and misinterpreting it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Cosine Similarity
T On Friday, December 7, 2012 9:47:46 AM UTC+5:30, Miki Tebeka wrote: > 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 Thanks Miki. It worked. Regards,Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
empty object from C
Hello, >From C, I'd like to call a Python function that takes an object and sets some attributes on it. Lets say this is the function... def foo(msg): msg.bar = 123 msg.spam = 'eggs' How do I create an empty object in C? In Python I would do something like this... class Msg(object): pass ... and then instantiate an instance, and call the function. msg = Msg() foo(msg) I know how to create an empty dictionary and I get get by with that, but I'd like to create an object. Thanks, ~Eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused compare function :)
On 2012-12-07, Steven D'Aprano wrote: > 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. I agree. I personally wouldn't want, e.g., 12.0 to get silently skipped so I've never done this. I've never done anything like that, but I can imagine it. > 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. What happens in Word during a Mail Merge if an invalid field is in the data file, one you don't even care about: You get to click on a modular dialog box for every record you're merging with to say IGNORE. And you can't quit. > 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. You have to draw the line somewhere in any case, and drawing it all the way over to IGNORE is bound right some of the time. It would be, I guess, Cargo Cult programming to never ignore errors. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: mini browser with python
On Thursday, December 06, 2012 03:15:42 PM Terry Reedy wrote: > 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. What you are suggesting is what was suggested when this project was originally discussed. However, I was naive about the complications of simply viewing a web page. And, making something like this work on Linux and Windows. I've come to the conclusion that processing the data with a parser and formatting it in a widget is the only way to make it work on both platforms. Parsing the data also gives one the ability to produce a module that will work in multiple versions of either platform and still keep control of all of the code. I appreciate the Help, Regards, jimonlinux -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3.3 urllib.request
On 7/12/12 13:52:52, Steeve C wrote: > hello, > > I have a python3 script with urllib.request which have a strange > behavior, here is the script : > > + > #!/usr/bin/env python3 > # -*- coding: utf-8 -*- > > import urllib.request > import sys, time > > > url = 'http://google.com' > > def make_some_stuff(page, url): > sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") > + url + "\"\n") > sys.stderr.write(str(page) + "\"\n") > return True > > def get_page(url): > while 1: > try: > page = urllib.request.urlopen(url) > yield page > > except urllib.error.URLError as e: > sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> > impossible to access to \"") + url + "\"\n") > time.sleep(5) > continue > > def main(): > print('in main') > for page in get_page(url): > make_some_stuff(page, url) > time.sleep(5) > > if __name__ == '__main__': > main() > + > > if the computer is connected on internet (with an ethernet connection > for example) and I run this script, it works like a charme : > - urllib.request.urlopen return the page > - make_some_stuff write in stderr > - when the ethernet cable is unplug the except block handle the error > while the cable is unplug, and when the cable is pluged > back urllib.request.urlopen return the page and make_some_stuff write in > stderr > > this is the normal behavior (for me, imho). > > but if the computer is not connected on internet (ethernet cable > unpluged) and I run this script, the except block handle the error > (normal), but when I plug the cable, the script continue looping > and urllib.request.urlopen never return the page (so, it always > go to the except block) > > What can I do to handle that ? On my laptop, your script works as you'd hope: if I plug in the network cable, then the next urllib request sometimes fails, but the request after that succeeds. This is using Python 3.3 on MacOS X 10.5. What version are you running? What happens if you start the script with the network cable plugged in, then unplug it when the first request has succeeded, and then plug it in again when the next request has failed? -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: KAJOL SEX VIDEOS'''''''''''''''''''
On Thursday, November 29, 2012 8:17:27 PM UTC-8, mentham...@gmail.com wrote: > -- http://mail.python.org/mailman/listinfo/python-list
Re: empty object from C
Eric Frederich, 07.12.2012 16:42: > From C, I'd like to call a Python function that takes an object and sets > some attributes on it. > Lets say this is the function... > > def foo(msg): > msg.bar = 123 > msg.spam = 'eggs' > > How do I create an empty object in C? > In Python I would do something like this... > > class Msg(object): > pass > > ... and then instantiate an instance, and call the function. > > msg = Msg() > foo(msg) > > I know how to create an empty dictionary and I get get by with that, but > I'd like to create an object. Here's some Cython code that you can call from C (after linking it in and creating a Python interpreter at runtime): cdef public set_attributes(msg, int bar, char* spam): msg.bar = bar msg.spam = spam.decode('utf8') # or whatever you use However, how you'd use it depends on your actual C code. Personally, I'd drop C and write all Python interaction in Cython. Makes these things way easier. Here are some longer examples: https://github.com/cython/cython/tree/master/Demos/embed https://github.com/cython/cython/tree/master/Demos/callback Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about readability
On Dec 7, 6:46 pm, Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > result = sum(int(data) for data in line.split(';')) > print(result) > > that sums the elements of the lines of this file: > > $ more data.txt > 30;44;99;88 > 11;17;16;50 > 33;91;77;15 > $ python3.3 myscript.py > 261 > 94 > 216 > > is explicit enough? Do you prefer a clearer solution? > Thanks in advance, Marco > -- > Marco Interpreting your question as a general question of stylistics, my experience is that a 3 line script often becomes a 10 line or a 50 line script at which point the direct printing will have to be modified to create an internal data structure. So on the whole I find it expedient to start with that assumption and write it as: def linesums(file): return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')] Whether this one-liner is readable or not and how to make it more so etc is probably bikeshedding. More important questions are for example: - Should you protect the open with a try - When to list and when to generate(or) All these are nice features of python but can lead to over-engineered code -- http://mail.python.org/mailman/listinfo/python-list
wrong ImportError message printed by python3.3 when it can't find a module?
Hi, I'm seeing that Python 3.3.0 is not printing the correct ImportError when it can't import a module that is imported from another module. Instead of printing the name of the module it can't import, it prints the name of the module that is doing the faulty import. Behold: I have created: importfail\ __init__.py main.py sub.py [L:\]type importfail\main.py from . import sub [L:\]type importfail\sub.py import nonexisting_module [L:\] [L:\]c:\Python33\python.exe -m importfail.main Traceback (most recent call last): File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "C:\Python33\lib\runpy.py", line 73, in _run_code exec(code, run_globals) File ".\importfail\main.py", line 1, in from . import sub ImportError: cannot import name sub# <--- huh ? Python 3.2 and earlier do the right thing: [L:\]c:\Python32\python.exe -m importfail.main Traceback (most recent call last): File "C:\Python32\lib\runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "C:\Python32\lib\runpy.py", line 73, in _run_code exec(code, run_globals) File "L:\importfail\main.py", line 1, in from . import sub File "importfail\sub.py", line 1, in import nonexisting_module ImportError: No module named nonexisting_module# <--- ok. (this is on windows, but on osx I see the same behavior). Is there a problem with the rewritten import logic in Python 3.3? Thanks Irmen de Jong -- http://mail.python.org/mailman/listinfo/python-list
Re: wrong ImportError message printed by python3.3 when it can't find a module?
Irmen de Jong wrote: > I'm seeing that Python 3.3.0 is not printing the correct ImportError when > it can't import a module that is imported from another module. Instead of > printing the name of the module it can't import, it prints the name of the > module that is doing the faulty import. > > Behold: > I have created: > importfail\ > __init__.py > main.py > sub.py > > > [L:\]type importfail\main.py > from . import sub > > [L:\]type importfail\sub.py > import nonexisting_module > > [L:\] > > > [L:\]c:\Python33\python.exe -m importfail.main > Traceback (most recent call last): > File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main > "__main__", fname, loader, pkg_name) > File "C:\Python33\lib\runpy.py", line 73, in _run_code > exec(code, run_globals) > File ".\importfail\main.py", line 1, in > from . import sub > ImportError: cannot import name sub# <--- huh ? > > > Python 3.2 and earlier do the right thing: > > [L:\]c:\Python32\python.exe -m importfail.main > Traceback (most recent call last): > File "C:\Python32\lib\runpy.py", line 160, in _run_module_as_main > "__main__", fname, loader, pkg_name) > File "C:\Python32\lib\runpy.py", line 73, in _run_code > exec(code, run_globals) > File "L:\importfail\main.py", line 1, in > from . import sub > File "importfail\sub.py", line 1, in > import nonexisting_module > ImportError: No module named nonexisting_module# <--- ok. > > (this is on windows, but on osx I see the same behavior). A paragon of clarity -- ye lurkers, this is how a bug report should be. > Is there a problem with the rewritten import logic in Python 3.3? > > Thanks > Irmen de Jong I believe this is fixed, see http://bugs.python.org/issue15111 -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused compare function :)
On Thu, 06 Dec 2012 23:14:17 +1100, Chris Angelico wrote: > Setting up the try/except is a constant time cost, It's not just constant time, it's constant time and *cheap*. Doing nothing inside a try block takes about twice as long as doing nothing: [steve@ando ~]$ python2.7 -m timeit "try: pass > except: pass" 1000 loops, best of 3: 0.062 usec per loop [steve@ando ~]$ python2.7 -m timeit "pass" 1000 loops, best of 3: 0.0317 usec per loop > while the duplicated > search for k inside the dictionary might depend on various other > factors. It depends on the type, size and even the history of the dict, as well as the number, type and values of the keys. Assuming a built-in dict, we can say that in the absence of many collisions, key lookup can be amortized over many lookups as constant time. > 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), Don't be so sure -- the hash randomization algorithm for Python 3.3 is trivially beaten by an attacker. http://bugs.python.org/issue14621#msg173455 but in general, yes, key lookup in dicts is fast. But not as fast as setting up a try block. Keep in mind too that the "Look Before You Leap" strategy is fundamentally unsound if you are using threads: # in main thread: if key in mydict: # returns True x = mydict[key] # fails with KeyError How could this happen? In the fraction of a second between checking whether the key exists and actually looking up the key, another thread could delete it! This is a classic race condition, also known as a Time Of Check To Time Of Use bug. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: wrong ImportError message printed by python3.3 when it can't find a module?
On 7-12-2012 22:20, Peter Otten wrote: > A paragon of clarity -- ye lurkers, this is how a bug report should be. :-) > >> Is there a problem with the rewritten import logic in Python 3.3? >> >> Thanks >> Irmen de Jong > > I believe this is fixed, see http://bugs.python.org/issue15111 Argh, why didn't I search the bug tracker first: I would have found that one for sure. Anyway, thanks for pointing it out. The bug is confusing but it doesn't break anything so far. I guess I'll be fine waiting for 3.3.1. Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3.3 urllib.request
On 12/7/2012 12:27 PM, Hans Mulder wrote: On 7/12/12 13:52:52, Steeve C wrote: hello, I have a python3 script with urllib.request which have a strange behavior, here is the script : + #!/usr/bin/env python3 # -*- coding: utf-8 -*- import urllib.request import sys, time url = 'http://google.com' def make_some_stuff(page, url): sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") + url + "\"\n") sys.stderr.write(str(page) + "\"\n") return True def get_page(url): while 1: try: page = urllib.request.urlopen(url) yield page except urllib.error.URLError as e: sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> impossible to access to \"") + url + "\"\n") time.sleep(5) continue def main(): print('in main') for page in get_page(url): make_some_stuff(page, url) time.sleep(5) if __name__ == '__main__': main() + if the computer is connected on internet (with an ethernet connection for example) and I run this script, it works like a charme : - urllib.request.urlopen return the page - make_some_stuff write in stderr - when the ethernet cable is unplug the except block handle the error while the cable is unplug, and when the cable is pluged back urllib.request.urlopen return the page and make_some_stuff write in stderr this is the normal behavior (for me, imho). but if the computer is not connected on internet (ethernet cable unpluged) and I run this script, the except block handle the error (normal), but when I plug the cable, the script continue looping and urllib.request.urlopen never return the page (so, it always go to the except block) What can I do to handle that ? Don't do that '-). On my laptop, your script works as you'd hope: if I plug in the network cable, then the next urllib request sometimes fails, but the request after that succeeds. This is using Python 3.3 on MacOS X 10.5. What version are you running? What happens if you start the script with the network cable plugged in, then unplug it when the first request has succeeded, and then plug it in again when the next request has failed? I believe he said that that worked. But unplugging cables is not a good idea ;-) I remember when it was recommended that all cables be plugged in and the the connected devices turned on when the computer was turned on and when devices might not be recognized unless plugged in and on when the computer was booted or rebooted. In other words, ports were scanned once as part of the boot process and adding a device required a reboot. It certainly was not that long ago when I had to reboot after the Internet Service went down and the cable modem had to reset. Ethernet and usb ports and modern OSes are more forgiving. But it does not surprise me if on some systems something has to be presence at process startup to evet be visible to the process. I believe this is all beyond Python's control. So the only thing to do might be to change hardware and/or OS or have the program restart itself if it gets repeated errors. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused compare function :)
On 12/7/2012 5:16 PM, Steven D'Aprano wrote: On Thu, 06 Dec 2012 23:14:17 +1100, Chris Angelico wrote: Setting up the try/except is a constant time cost, It's not just constant time, it's constant time and *cheap*. Doing nothing inside a try block takes about twice as long as doing nothing: [steve@ando ~]$ python2.7 -m timeit "try: pass except: pass" 1000 loops, best of 3: 0.062 usec per loop [steve@ando ~]$ python2.7 -m timeit "pass" 1000 loops, best of 3: 0.0317 usec per loop while the duplicated search for k inside the dictionary might depend on various other factors. It depends on the type, size and even the history of the dict, as well as the number, type and values of the keys. Assuming a built-in dict, we can say that in the absence of many collisions, key lookup can be amortized over many lookups as constant time. 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), Don't be so sure -- the hash randomization algorithm for Python 3.3 is trivially beaten by an attacker. http://bugs.python.org/issue14621#msg173455 but in general, yes, key lookup in dicts is fast. But not as fast as setting up a try block. Keep in mind too that the "Look Before You Leap" strategy is fundamentally unsound if you are using threads: # in main thread: if key in mydict: # returns True x = mydict[key] # fails with KeyError How could this happen? In the fraction of a second between checking whether the key exists and actually looking up the key, another thread could delete it! This is a classic race condition, also known as a Time Of Check To Time Of Use bug. I generally agree with everything Steven has said here and in previous responses and add the following. There are two reasons to not execute a block of code. 1. It could and would run, but we do not want it to run because a) we do not want an answer, even if correct; b) it would return a wrong answer (which of course we do not want); or c) it would run forever and never give any answer. To not run code, for any of these reasons, requires an if statement. 2. It will not run but will raise an exception instead. In this case, we can always use try-except. Sometimes we can detect that it would not run before running it, and can use an if statement instead. (But as Steven points out, this is sometimes trickier than it might seem.) However, even if we can reliably detect that code would either run or raise an exception, this often or even usually requires doing redundant calculation. For example, 'key in mydict' must hash the key, mod the hash according to the size of the dict, find the corresponding slot in the dict, and do an equality comparison with the existing key in the dict. If not equal, repeat according to the collision algorithm for inserting keys. In other words, 'key in mydict' does everything done by 'mydict[key]' except to actually fetch the value when the right slot is found or raise an exception if there is no right slot. So why ever use a redundant condition check? A. esthetics. B. practicality. Unfortunately, catching exceptions may be and often is as slow as the redundant check and even multiple redundant checks. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused compare function :)
On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy wrote: > Unfortunately, catching exceptions may be and often is as slow as the > redundant check and even multiple redundant checks. It depends on how often you're going to catch and how often just flow through. In Python, as in most other modern languages, exceptions only cost you when they get thrown. The extra check, though, costs you in the normal case. ChrisA -- http://mail.python.org/mailman/listinfo/python-list