Re: [Tutor] output sequentially
lina wrote: > I have below file, > > I wish the output following: > > The first field 169 -170 sequential, and then the filed 2 from 1-29, > ignore the rest 4 fields, > > 169CHOL O28 1612 6.966 6.060 6.429 Read the lines from the file, sort them with a proper key function, write the sorted lines to a new file: with open("source.txt") as instream: lines = sorted(instream, key=mykey) with open("dest.txt", "w") as outstream: outstream.writelines(lines) Now for the mykey() function: what should it look like? You want to sort by the integer value of the first two columns, so you have to split the lines into fields and then remove the non-digits from the fields you are interested in. Here's an outline: def extract_int(field): only_digits = ... return int(only_digits) assert extract_int("169CHOL") == 169 assert extract_int("H28") == 28 def mykey(line): fields = ... # example: ['169CHOL', 'H29', '1611', '6.963', '6.155', '6.395'] return extract_int(fields[0]), extract_int(fields[1]) assert mykey("169CHOL H29 1611 6.963 6.155 6.395\n") == (169, 28) Can you fill in the blanks? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] output sequentially
Hi, I have below file, I wish the output following: The first field 169 -170 sequential, and then the filed 2 from 1-29, ignore the rest 4 fields, thanks for any suggestions, 169CHOL H29 1611 6.963 6.155 6.395 169CHOL O28 1612 6.966 6.060 6.429 169CHOLC3 1613 7.005 6.073 6.566 169CHOLC4 1614 6.940 5.950 6.629 169CHOLC5 1615 7.006 5.934 6.766 169CHOL C10 1616 6.980 6.060 6.849 169CHOLC1 1617 7.040 6.182 6.781 169CHOLC2 1618 6.973 6.201 6.645 169CHOLC6 1619 7.055 5.819 6.816 169CHOLC7 1620 7.089 5.824 6.965 169CHOLC8 1621 7.000 5.916 7.048 169CHOLC9 1622 7.031 6.056 6.993 169CHOL C11 1623 7.000 6.169 7.092 169CHOL C12 1624 7.050 6.144 7.235 169CHOL C13 1625 6.993 6.009 7.281 169CHOL C14 1626 7.048 5.897 7.193 169CHOL C15 1627 6.982 5.781 7.266 169CHOL C16 1628 7.008 5.803 7.414 169CHOL C17 1629 7.038 5.952 7.414 169CHOL C19 1630 6.830 6.092 6.846 169CHOL C18 1631 6.841 6.000 7.265 169CHOL C20 1632 6.983 6.033 7.532 169CHOL C21 1633 7.094 6.057 7.635 169CHOL C22 1634 6.862 5.976 7.605 169CHOL C23 1635 6.838 6.050 7.737 169CHOL C24 1636 6.753 5.958 7.824 169CHOL C25 1637 6.609 6.008 7.837 169CHOL C26 1638 6.537 5.895 7.911 169CHOL C27 1639 6.620 6.116 7.945 170CHOL H29 1640 5.603 6.375 6.159 170CHOL O28 1641 5.625 6.472 6.165 170CHOLC3 1642 5.604 6.501 6.303 170CHOLC4 1643 5.560 6.645 6.330 170CHOLC5 1644 5.539 6.683 6.477 170CHOL C10 1645 5.670 6.664 6.552 170CHOLC1 1646 5.716 6.520 6.531 170CHOLC2 1647 5.732 6.480 6.384 170CHOLC6 1648 5.427 6.733 6.530 170CHOLC7 1649 5.430 6.768 6.679 170CHOLC8 1650 5.569 6.795 6.736 170CHOLC9 1651 5.661 6.676 6.704 170CHOL C11 1652 5.792 6.713 6.775 170CHOL C12 1653 5.781 6.703 6.927 170CHOL C13 1654 5.695 6.824 6.964 170CHOL C14 1655 5.564 6.799 6.889 170CHOL C15 1656 5.462 6.893 6.952 170CHOL C16 1657 5.506 6.897 7.097 170CHOL C17 1658 5.637 6.821 7.105 170CHOL C19 1659 5.765 6.766 6.489 170CHOL C18 1660 5.769 6.948 6.913 170CHOL C20 1661 5.727 6.885 7.211 170CHOL C21 1662 5.832 6.780 7.247 170CHOL C22 1663 5.655 6.910 7.343 170CHOL C23 1664 5.749 6.980 7.441 170CHOL C24 1665 5.667 6.987 7.570 170CHOL C25 1666 5.745 7.071 7.672 170CHOL C26 1667 5.674 7.205 7.695 170CHOL C27 1668 5.760 6.998 7.805 -- Best Regards, lina ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Don't understand this class/constructor call syntax
dave wrote: Is it even possible to replace the implicit self argument of the initializer by passing something else? If so, what would be the syntax. Yes, by calling an "unbound method". Consider this class: class MyClass: def func(self, x): return x+1 When you run this code, Python creates a class object MyClass, containing one method object, "func". Notice that the syntax for defining a method is the same as defining a function, except that it is inside a class. In fact they are the same things, except that methods are a lightweight wrapper around the real function object. We can see this by inspecting the method: >>> instance = MyClass() >>> instance.func > The method wrapper is responsible for automatically providing the "self" argument when you call the function on the instance. The terminology is that the method is "bound" to the instance, so the wrapper knows what to give as self. When you call the method instance.func(42), the wrapper calls the underlying function func(instance, 42). (Aside: Python also provides two other kinds of methods, classmethod and staticmethod, which do something different. And of course the machinery that makes this work is available for you to write your own custom method types, if you can think of a need.) But you can also access the method directly from the class object: >>> MyClass.func In this case, the method wrapper doesn't have access to an instance to use as "self" -- it is "unbound". (Note for Python 3, it was determined that there is no need to provide unbound methods anymore, and the underlying function object is returned instead.) Before you can actually call the function, you need to provide an argument for "self". You can do that by hand: >>> MyClass.func(instance, 42) 43 In Python 2, the unbound method wrapper enforces that the first argument is actually an instance of MyClass. But in Python 3 unbound methods are gone, and so you can pass anything you like. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between %s and %r?
Hello! Thank you all for writing and helping me out. I now understand the difference between the two format characters. -amt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logger object not passed between modules
Luke Thomas Mergner wrote: > I am very new to Python and programming, basically just a curious > hobbyist. I am building a learning app that hopefully will include a > wxPython GUI. Right now I am trying to understand my code better by > including print statements. Now I know that I could just print to the > terminal, but I thought 'why not try this nice little logger class.' And > it works perfectly in the main class App(wx.App), but I can't seem to pass > the same logger object to the imported modules. I'm verifying this by > printing the logger object to terminal (stdout?). I've spent close to 6 > hours trying to figure this out, and I need some help. > > The logger object is created within a function in the main app class. Is > this wrong? I thought that logger ensured the same object is used by both > classes. The logger.getLogger(__name__) doesn't set anything other than > the name used to describe the source of the message, so how do I make sure > I'm using the same log object? No, the name is used to identify a logger; different names mean different loggers. > I assume the problem is either scope (the > log has to be in the module, not the class) or passing the object > properly, neither of which I"m very comfortable with obviously. Loggers are put into a hierarchy similar to a directory tree with the root logger at the top. By default the other loggers pass logging messages up to their parent so that all messages (or LogRecords) are eventually seen by the root logger. In most cases it is sufficient to handle them there, and the easiest way to add a suitable formatter and handler is logging.basicConfig(): >>> import logging >>> class App: ... def __init__(self): ... self.logger = logging.getLogger("main") ... >>> class Frame: ... def __init__(self): ... self.logger = logging.getLogger("frame") ... >>> logging.basicConfig(level=logging.DEBUG, filename="tmp.log") >>> app = App() >>> frame = Frame() >>> frame.logger.info("hello from frame") >>> app.logger.info("hello from app") >>> with open("tmp.log") as f: print f.read() ... INFO:frame:hello from frame INFO:main:hello from app ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logger object not passed between modules
Hi Luke, On 26 July 2011 06:58, Luke Thomas Mergner wrote: > The logger object is created within a function in the main app class. Is > this wrong? I thought that logger ensured the same object is used by both > classes. The logger.getLogger(__name__) doesn't set anything other than the > name used to describe the source of the message, so how do I make sure I'm > using the same log object? I assume the problem is either scope (the log > has to be in the module, not the class) or passing the object properly, > neither of which I"m very comfortable with obviously. > With the caveat that I'm not exactly a logging expert in Python, let me submit the following observations: 1) You should generally only create logger objects inside modules (whether main or otherwise) for module specific logging. Such a created logger should not be shared with other modules. So yes your current approach at the very least doesn't go by the the logging module's normal usage pattern(s). 2) If you don't care about module specific logging (and I suggest you do not, at this stage of your learning), then you should ignore creating your own logger objects and instead, simply directly log using the logging *module* (which contains a share loggger object already.) 3) See the section entitled "Logging from multiple modules" here: http://docs.python.org/howto/logging.html for an example of this. It demonstrates how you can do some basic configuration of your log in the main application module and also log from other modules, all without having to explicitly create your own (module specific) logger objects. HTH, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Don't understand this class/constructor call syntax
dave wrote: Is it even possible to replace the implicit self argument of the initializer by passing something else? If so, what would be the syntax. Im not sure this is what you mean but... When you call a method on an object like: class MyClass: def aMethod(self,spam): pass anObject= MyClass() anObject.aMethod(42) You could replace the last line with: MyClass.aMethod(anObject, 42) This explicitly specifies the value of self in aMethod() So you could in theory pass any object into the method, although in most cases it would result in an error. Is that what you mean? Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor