Re: % string formatting - what special method is used for %d?
Ian Kelly wrote: > On Sat, Dec 10, 2016 at 11:40 PM, Veek M wrote: >> Well take a look at this: >> ### >> #!/usr/bin/python >> >> class Foo(int): >> def __init__(self, value): >> self.value = value >> >> def __str__(self): >> print '__str__' >> return str(self.value) >> >> def __int__(self): >> print '__int__' >> return self.value + 1 >> >> >> #'%s' % Foo(10) # %s is mapped to __str__ >> '%d' % Foo(20) >> ### >> >> here, '__str__' prints because when you do: >> '%s' % x >> the __str__ method is invoked. So internally %s invokes __str__ >> independent of print. >> >> However the next line doesn't trigger any similar invocation with >> __int__ or__str__? (but int(Foo(10)) would invoked __int__) > > This is probably because Foo inherits from int. Foo(20) is already an > int so there is no conversion to be done; Python simply uses the int > value and ignores the __int__ method in this case. > >> Is there a way to trigger special methods using %d etc OR is this >> restricted to %s and why? > > For an object that is already an int, probably not. > > However you may want to revisit your decision to make Foo inherit from > int and question whether that is really sensible if you're also > wanting to override the __int__ method. What does that mean if > something is an int but also provides a method to convert to int? It's > a contradiction. Ah! thanks guys, now it works great. (I still need to ponder all this anyhow but yay!) -- https://mail.python.org/mailman/listinfo/python-list
Re: Help in creating a dynamic/loop based on variables and CSV files
Umar Yusuf wrote: > Hi all, > I need your help with any of these questions? > > 1- > http://stackoverflow.com/questions/41083699/python-create-dynamic-loop-based-on-variables-and-csv You should really make a serious attempt to explain the problem in plain english. Throwing a piece of code at other people is usually not enough to get a meaningful answer. That said, my crystal ball tells me that you want to remove all rows of a csv before the one containing a keyword in the first column. You can do it for one keyword/csv-infile/csv-outfile triple, > with open("keywords.txt", "rb") as keywords, open('folding umbrella-sort- highest.csv', 'rb') as g, open('lego minecraft-sort-highest.csv', 'rb') as f, open('filename1.csv', 'wb') as myfile1, open('filename2.csv', 'wb') as myfile2: > > # Step1: Read contents of keywords.tex in variables > ky = list(keywords.readlines()) > ky1, ky2 = ky[0], ky[1] > > # Step2: Read and process folding umbrella-sort-highest.csv > reader = csv.reader(g) > umbrella_list = list(reader) > list1 = filter(lambda e: e[0] in ky1, umbrella_list) > > list2 = list(chain(*list1)) > # or better: (available since Python 2.6) > # print list(chain.from_iterable(list1)) > > ind_prt1 = umbrella_list.index(list2) +1 > mylist1 = umbrella_list[:ind_prt1] > > for r in mylist1: > wr = csv.writer(myfile1, quoting=csv.QUOTE_ALL) > wr.writerow(r) > and by employing copy-and-paste coding you have managed to do it for two keywords. Now you want to generalize it for an arbitrary number of triples. The usual approach is to put the code that works for one instance into a function and call that for every set of arguments def process_one(keyword, infile, outfile): # your code for keyword, infile, outfile in triples: process_one(keyword, infile, outfile) Where can you get those triples? Rather than putting just the keywords into a text file i would use a csv with three columns: first_keyword,folding umbrella-sort-highest.csv,filename1.csv second_keyword,lego minecraft-sort-highest.csv,filename2.csv ... Then the calling code becomes with open("keywords.csv", "rb") as f: for keyword, infile, outfile in csv.reader(f): process_one(keyword, infile, outfile) Now to the contents of process_one(). Your code is very complicated. If it does what I think it can be rewritten as def process_one(keyword, infile, outfile): with open(outfile, "wb") as outstream: writer = csv.writer(outstream, csv.QUOTE_ALL) with open(infile, "rb") as instream: for row in csv.reader(instream): writer.writerow(row) if row[0] in keyword: break By the way, the row[0] in keyword test looks odd. Should that be keyword in row[0] or keyword == row[0] ? Without a proper description of the problem that led to your code I have no way to tell. > 2- > http://stackoverflow.com/questions/41081800/python-pandas-how-to-use-dataframe-cell-to-search-another-dataframe-column-and > > Thanks in advance for your time. -- https://mail.python.org/mailman/listinfo/python-list
Re: CLP stats: last 500 posts
On Sat, 10 Dec 2016 12:31:33 -0500, DFS wrote: > After correcting my stupid oversights, the code runs fine up to the point where the user agents are printed. I get an error saying that 'User-Agent' is an unsupported header field. It must have something to do with giganews. If I use aioe.org I don't get the error and the user agents are printed. I don't think it is a problem with the code but any thoughts why giganews is not playing nice? And it is not related to the python group. I have tried on other groups and i get the same error. Here is the complete error message. Traceback (most recent call last): File "./nntp.py", line 27, in printStat("User-Agent","User-Agents",m) File "./nntp.py", line 12, in printStat r,d=n.xhdr(st,'%s-%s'%rg) File "/usr/lib/python2.7/nntplib.py", line 470, in xhdr resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str, file) File "/usr/lib/python2.7/nntplib.py", line 273, in longcmd return self.getlongresp(file) File "/usr/lib/python2.7/nntplib.py", line 244, in getlongresp resp = self.getresp() File "/usr/lib/python2.7/nntplib.py", line 229, in getresp raise NNTPPermanentError(resp) nntplib.NNTPPermanentError: 501 unsupported header field -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
pySerial raw data
I have an outdoor thermometer that transmits to an indoor receiver at 433Mhz. I also have a 433Mhz USB serial port jig from a TI development tool. I would like to use the TI USB serial port to capture the temperature information. The TI USB port registers as a COM port that I can access with pySerial. Now the datasheet from the temperature probe only says that the RF frequency is 433MHz and that it transmits every 39 seconds. Since I don't know what protocol the thermometer uses or baud rate, I want to look at the rawest level of data collected with the USB com port and see if I can make anything out of the gobbledy gook coming in. Is there a way to get this kind of data from pySerial? I've tried scanning at different baud rates but so far I haven't captured anything. Also in the advanced settings in windows device manager, there are some settings for Fifo buffers, and receive and transmit buffers. Can these be accessed in pySerial? Does pySerial override the settings for baud rate, etc in windows device manager or do I need to set those to match what I'm using in pySerial? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: pySerial raw data
On 2016-12-11 16:28, Wanderer wrote: I have an outdoor thermometer that transmits to an indoor receiver at 433Mhz. I also have a 433Mhz USB serial port jig from a TI development tool. I would like to use the TI USB serial port to capture the temperature information. The TI USB port registers as a COM port that I can access with pySerial. Now the datasheet from the temperature probe only says that the RF frequency is 433MHz and that it transmits every 39 seconds. Since I don't know what protocol the thermometer uses or baud rate, I want to look at the rawest level of data collected with the USB com port and see if I can make anything out of the gobbledy gook coming in. Is there a way to get this kind of data from pySerial? I've tried scanning at different baud rates but so far I haven't captured anything. Also in the advanced settings in windows device manager, there are some settings for Fifo buffers, and receive and transmit buffers. Can these be accessed in pySerial? Does pySerial override the settings for baud rate, etc in windows device manager or do I need to set those to match what I'm using in pySerial? What is the make and model of the thermometer? Is the datasheet online somewhere? -- https://mail.python.org/mailman/listinfo/python-list
PyGreSQL 5.0.3 released
Release 5.0.3 of PyGreSQL. It is available at: http://pygresql.org/files/PyGreSQL-5.0.3.tar.gz. If you are running NetBSD, look in the packages directory under databases. There is also a package in the FreeBSD ports collection. Please refer to the changelog.txt file for things that have changed in this version. Please refer to `readme.txt for general information. This version has been built and unit tested on: - NetBSD - FreeBSD - openSUSE - Ubuntu - Windows 7 with both MinGW and Visual Studio - PostgreSQL 9.0 to 9.5 32 and 64bit - Python 2.6, 2.7, 3.3, 3.4 and 3.5 32 and 64bit -- D'Arcy J.M. Cain PyGreSQL Development Group http://www.PyGreSQL.org IM:da...@vex.net -- https://mail.python.org/mailman/listinfo/python-list
The right way to 'call' a class attribute inside the same class
I'm watching a Python course and was presented a topic regarding classes. One of the examples were: box.py class Box: serial = 100 def __init__(self, from_addr, to_addr): self.from_addr = from_addr self.to_addr = to_addr self.serial = Box.serial Box.serial += 1 from box import * a = Box('19 Beech Ave. Seattle, WA 98144', '49 Carpenter Street North Brunswick, NJ 08902') b = Box('68 N. Church Dr. Vicksburg, MS 39180', '8 Lake Forest Road Princeton, NJ 08540') print(a.serial) # print: 100 print(b.serial) # print: 101 print(Box.serial) # print: 102 The instructor said that the right way to call a class attribute is to use 'Class.class_attr' notation, but on the web I found examples where people used 'self.class_attr' to call class attributes. I believe that using the first notation is better ('Class.class_attr'), this way the code is more explicit, but is there any rules regarding it? -- https://mail.python.org/mailman/listinfo/python-list
Re: CLP stats: last 500 posts
On 2016-12-11, Wildman wrote: > I don't think it is a problem with the code but any thoughts > why giganews is not playing nice? Most likely because you're calling XHDR on a header which is not in the server's overview file. -- https://mail.python.org/mailman/listinfo/python-list
Re: pySerial raw data
On Sunday, December 11, 2016 at 12:52:04 PM UTC-5, MRAB wrote: > On 2016-12-11 16:28, Wanderer wrote: > > I have an outdoor thermometer that transmits to an indoor receiver at > > 433Mhz. I also have a 433Mhz USB serial port jig from a TI development > > tool. I would like to use the TI USB serial port to capture the temperature > > information. The TI USB port registers as a COM port that I can access with > > pySerial. Now the datasheet from the temperature probe only says that the > > RF frequency is 433MHz and that it transmits every 39 seconds. Since I > > don't know what protocol the thermometer uses or baud rate, I want to look > > at the rawest level of data collected with the USB com port and see if I > > can make anything out of the gobbledy gook coming in. Is there a way to get > > this kind of data from pySerial? I've tried scanning at different baud > > rates but so far I haven't captured anything. > > > > Also in the advanced settings in windows device manager, there are some > > settings for Fifo buffers, and receive and transmit buffers. Can these be > > accessed in pySerial? Does pySerial override the settings for baud rate, > > etc in windows device manager or do I need to set those to match what I'm > > using in pySerial? > > > What is the make and model of the thermometer? Is the datasheet online > somewhere? http://global.oregonscientific.com/manual/THN132N.pdf -- https://mail.python.org/mailman/listinfo/python-list
Re: pySerial raw data
Wanderer writes: > I also have a 433Mhz USB serial port jig from a TI development > tool The TI USB port registers as a COM port that I can access > with pySerial. If the TI jig has 433 mhz (LORA?) at one end and serial at the other, you have to find the port parameters in the docs for the TI jig, not the thermometer. If you don't have docs you can often figure out the right settings by trial and error. If not, the direct approach is to use an oscilloscope. -- https://mail.python.org/mailman/listinfo/python-list
Re: pySerial raw data
On 2016-12-11 21:29, Wanderer wrote: On Sunday, December 11, 2016 at 12:52:04 PM UTC-5, MRAB wrote: On 2016-12-11 16:28, Wanderer wrote: > I have an outdoor thermometer that transmits to an indoor receiver at 433Mhz. I also have a 433Mhz USB serial port jig from a TI development tool. I would like to use the TI USB serial port to capture the temperature information. The TI USB port registers as a COM port that I can access with pySerial. Now the datasheet from the temperature probe only says that the RF frequency is 433MHz and that it transmits every 39 seconds. Since I don't know what protocol the thermometer uses or baud rate, I want to look at the rawest level of data collected with the USB com port and see if I can make anything out of the gobbledy gook coming in. Is there a way to get this kind of data from pySerial? I've tried scanning at different baud rates but so far I haven't captured anything. > > Also in the advanced settings in windows device manager, there are some settings for Fifo buffers, and receive and transmit buffers. Can these be accessed in pySerial? Does pySerial override the settings for baud rate, etc in windows device manager or do I need to set those to match what I'm using in pySerial? > What is the make and model of the thermometer? Is the datasheet online somewhere? http://global.oregonscientific.com/manual/THN132N.pdf That datasheet says """This product is compatible with various wireless weather station products.""" OK, so that suggests that there's a standard of some kind somewhere. Googling for """wireless weather station protocol""" gives: Reverse engineering wireless weather stations hackaday.com/2011/06/13/reverse-engineering-wireless-weather-stations/ and that page leads to: TX29 Protocol http://fredboboss.free.fr/articles/tx29.php Good luck! -- https://mail.python.org/mailman/listinfo/python-list
Re: The right way to 'call' a class attribute inside the same class
On Mon, Dec 12, 2016 at 7:10 AM, Juan C. wrote: > class Box: > serial = 100 > > def __init__(self, from_addr, to_addr): > self.from_addr = from_addr > self.to_addr = to_addr > self.serial = Box.serial > Box.serial += 1 > I would say that this is awkward usage; the class attribute isn't being used as a default for the instance, it's being used as "the next one". I would rename the class attribute to "next_serial". That would give you the freedom to use whichever notation you like, as there won't be a conflict. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: The right way to 'call' a class attribute inside the same class
Juan C. wrote: The instructor said that the right way to call a class attribute is to use 'Class.class_attr' notation, but on the web I found examples where people used 'self.class_attr' to call class attributes. I believe that using the first notation is better ('Class.class_attr'), this way the code is more explicit, but is there any rules regarding it? It depends on how the class attribute is being used. If you're only reading the attribute, either way will work. Which one is more appropriate depends on what the attribute is used for. Often a class attribute is used as a default value for an instance attribute, in which case accessing it via the instance is entirely appropriate. On the other hand, if it's truly mean to be an attribute of the class itself, accessing it via the class is probably clearer. If the attribute is being written, you don't have any choice. If you want to rebind the attribute in the class, you have to access it via the class. This is the case for this line in your example: Box.serial += 1 If instead you did 'self.serial += 1' it would create a new instance attribute shadowing the class attribute, and the class attribute would remain bound to its previous value. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: The right way to 'call' a class attribute inside the same class
On 12/11/2016 5:29 PM, Gregory Ewing wrote: Juan C. wrote: The instructor said that the right way to call a class attribute is to use 'Class.class_attr' notation, but on the web I found examples where people used 'self.class_attr' to call class attributes. I believe that using the first notation is better ('Class.class_attr'), this way the code is more explicit, but is there any rules regarding it? Yes. Use the form appropriate to the situation. In other words, use open-eyed rule, not a closed-eye rule. This applies to much of Python programming and programming is general. Greg nicely explains the application of this rule. It depends on how the class attribute is being used. If you're only reading the attribute, either way will work. Which one is more appropriate depends on what the attribute is used for. Often a class attribute is used as a default value for an instance attribute, in which case accessing it via the instance is entirely appropriate. The use of a (constant) class attribute as default instance attribute might be an optimization added after the first version of the class, or one that could disappear in the future. > On the other hand, if it's truly mean to be an attribute of the class itself, accessing it via the class is probably clearer. If the attribute is being written, you don't have any choice. If you want to rebind the attribute in the class, you have to access it via the class. This is the case for this line in your example: Box.serial += 1 If instead you did 'self.serial += 1' it would create a new instance attribute shadowing the class attribute, and the class attribute would remain bound to its previous value. I agree with the other post suggesting using 'next_serial' as the class attribute, as that is what the class attribute is. I would access it as Box.serial. Instance methods should normal be accessed through an instance, though there are exceptions. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: The right way to 'call' a class attribute inside the same class
On Mon, 12 Dec 2016 07:10 am, Juan C. wrote: > I'm watching a Python course and was presented a topic regarding classes. > One of the examples were: > > box.py > > class Box: > serial = 100 > > def __init__(self, from_addr, to_addr): > self.from_addr = from_addr > self.to_addr = to_addr > self.serial = Box.serial > Box.serial += 1 If you want to distinguish between an instance attribute and a class attribute, you must specify the instance or the class: self.serial # may be the instance attribute, or the class attribute Box.serial # always the class attribute But what happens inside a subclass? class BoxWithLid(Box): pass Generally we expect methods called from the subclass BoxWithLid to refer to the BoxWithLid attribute, not the Box attribute. So Box.serial will be wrong when the method is called from a subclass. type(self).serial # still correct when called from a subclass > The instructor said that the right way to call a class attribute is to use > 'Class.class_attr' notation, That is nearly always wrong, since it will break when you subclass. If you do that, you should document that the class is not expected to be subclasses, and may not work correctly if you do. > but on the web I found examples where people > used 'self.class_attr' to call class attributes. I believe that using the > first notation is better ('Class.class_attr'), this way the code is more > explicit, but is there any rules regarding it? Assignment to self.serial will always create or affect an instance attribute. But merely retrieving self.serial will use inheritance to try returning the instance attribute *if it exists*, and if not, fall back on the class attribute (or a superclass). This is especially useful for read-only defaults, constants or configuration settings. class Document: page_size = A4 def page_area(self): dimensions = list(self.page_size) dimensions[0] -= self.left_margin dimensions[1] -= self.right_margin dimensions[2] -= self.top_margin dimensions[3] -= self.bottom_margin return dimensions doc = Document() # uses the default page size of A4 doc.page_size = Foolscape # override the default It is a matter of taste and context whether you do this, or the more conventional way: class Document: def __init__(self): self.page_size = A4 Use whichever is better for your specific class. So... in summary: When *assigning* to an attribute: - use `self.attribute = ...` when you want an instance attribute; - use `Class.attribute = ...` when you want a class attribute in the same class regardless of which subclass is being used; - use `type(self).attribute = ...` when you want a class attribute in a subclass-friendly way. When *retrieving* an attribute: - use `self.attribute` when you want to use the normal inheritance rules are get the instance attribute if it exists, otherwise a class or superclass attribute; - use `type(self).attribute` when you want to skip the instance and always return the class or superclass attribute. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: CLP stats: last 500 posts
On Sun, 11 Dec 2016 12:03:07 -0500, DFS wrote: > For this short stat version I only used the 'User-Agent' header. I have > a longer version that uses both 'User-Agent' and 'X-Newsreader' > > > You can put a conditional in place for now: > > if s='giganews': > printStat("X-Newsreader","News Readers",m) > else: > printStat("User-Agent","User-Agents",m) Thanks but I had already tried X-Newsreader and I got the same result. It is odd because if you look at my headers there is an entry for User-Agent User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2; x86_64-pc-linux-gnu) -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list