Add lists to class?
I have a list with some strings in in it, 'one', 'two' 'three' and so on. I would like to add lists to a class with those names. I have no way of knowing what will be in the list or how long the list will be in advance. Something like: class master: def __init__(self, list): self.count = len(list) for line in list: self.line = [] # obviously this doesn't work list = ['one', 'two', 'three'] a = master(list) so that I get: dir(a) ['_doc__', '_init__', '_module__', 'one', 'two', 'three'] instead of: dir(a) ['_doc__', '_init__', '_module__', 'line'] TIA, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Add lists to class?
Thanks to a generous Pyhtonista replied with a pointer to setattr(). jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Add lists to class?
tested and working... jab, now possessing an embarrassment of riches, says "Thanks!" -- http://mail.python.org/mailman/listinfo/python-list
Re: Add lists to class?
That's interesting and I take your point. Maybe there is a better way. Here is what I am doing now. (working) I start with a text file of ticker symbols. I read each symbol and stick it in a list, retrieve the data for it from MySQL, do a trivial smoothing and pass the data to a modeling routine. I read the tickers into a list, self.symbols. Then for each ticker I create a list, self._0, self._1, ... and a list for the smoothed values, self._0_smooth, self._1_smooth, ... I end up with data I can access with self.__dict__["_" + str(var)] and a matching symbol which I can access self.symbols[var]. Ideas for a better approach gladly accepted. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Add lists to class?
That's interesting and I take your point. Maybe there is a better way. Here is what I am doing now. (working) I start with a text file of ticker symbols. I read each symbol and stick it in a list, retrieve the data for it from MySQL, do a trivial smoothing and pass the data to a modeling routine. I read the tickers into a list, self.symbols. Then for each ticker I create a list, self._0, self._1, ... and a list for the smoothed values, self._0_smooth, self._1_smooth, ... I end up with data I can access with self.__dict__["_" + str(var)] and a matching symbol which I can access self.symbols[var]. Ideas for a better approach gladly accepted. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Add lists to class?
> Why don't you use a real list instead? I am using lists... I just showed the naming schema. Here is how they are implemented. for var in range(len(self.symbols)): setattr(self, "_" + str(var), []) > I don't understand what > self.__dict__["_" + str(var)] gets you. It let's me access lists that aren't known at write time. jab -- http://mail.python.org/mailman/listinfo/python-list
breaking up is hard to do
I've a 2,000 line and growing Python script that I'd like to break up into a modules--one class alone is currently over 500 lines. There is a large config.ini file involved (via ConfigParser), a fair number of passed and global variables as well as interaction with external programs such as MySQL (via MySQLdb), R (via Rpy) and gnuplot (via Gnuplot). Every time I have tried to break it up I end up with thorny name-space problems and have had to stitch it back together gain. Any pointers to materials on how best to modularize a script such as this would be appreciated. I'd like to end up with most of the code in modules and with just the main logic and housekeeping stuff in the main module. John PS The script works fine as is and I am a very happy and productive camper with Python. I was a BASIC user for many years who switched to Python a year and a half ago rather than moving to .NET. -- http://mail.python.org/mailman/listinfo/python-list
Re: breaking up is hard to do
For example I have a class named Indicators. If I cut it out and put it in a file call Ind.py then "from Ind import Indicators" the class can no longer see my globals. This is true even when the import occurs after the config file has been read and parsed. John -- http://mail.python.org/mailman/listinfo/python-list
Re: breaking up is hard to do
Thanks for your reply. That seems like an interesting and practical approach. However, I have one worry. In addition to the config file I am parsing command-line overrides to the config values via optparse. Many modules, classes and functions depend on these values, which means a lot of code duplication and changes that must be made in many places when a change is needed. It would seem simpler and more elegant to have all these values held by one global resource that is referenced whenever needed. Am I missing something? jab -- -- http://mail.python.org/mailman/listinfo/python-list
Re: breaking up is hard to do
Cameron, Thanks for the heads up on that. (I have been following it, but from a long distance, as I very happy with my garden-variety Python.) Separately, let me offer you my thanks for your contributions to the community; they are great and they are much appreciated. The community around Python was a significant reason in my switch decision, a decision I have had no reason to regret. Thanks again, jab -- http://mail.python.org/mailman/listinfo/python-list
locating strings approximately
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long list of songs and return any approximate matches along with a confidence factor. I have looked at edit distance, but that isn't a good choice for finding a short string in a longer one. I have also explored difflib.SequenceMatcher and .get_close_matches, but what I'd really like is something like: a = FindApprox("beatles", "beatlemania") print a 0.857 Any ideas? jab -- http://mail.python.org/mailman/listinfo/python-list
DDE (eSignal)
I have a Python ap that needs current stock prices, which I want to get from eSignal's DDE server. Following the win32all example: import win32ui import dde server = dde.CreateServer() server.Create("eSignalDDE") conversation = dde.CreateConversation(server) conversation.ConnectTo("WINROS", "Last") last = conversation.Request("$spx") print last Which almost works. The return in PythonWin is the correct price as a string with some extra chartacters appended. '1402.6700\x00\x12\x00*\x00\x00\x004\xfb\x12\x00\xfd\x1a\xd9w4\xc1\x00' Any thoughts on this? TIA, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: DDE (eSignal)
Thomas Heller wrote: > Looks like a bug, either in the dde module or the dde server. > But it's easy to find a workaround: > '1402.6700\x00\x12\x00*\x00\x00\x004\xfb\x12\x00\xfd\x1a\xd9w4\xc1\x00'.split("\0")[0] > '1402.6700' float(last.split("\0")[0]) works for me. Thanks for that! jab -- http://mail.python.org/mailman/listinfo/python-list
Reduced invective
My fat-fingered alter ego typed delete from iv where date > '2006-01-01'; instead of delete from iv where date > '2006-12-01'; leaving me with 50 tables to reload. :( estimated time to fix > several hours estimated invective dispensed during fix = classified Python to the rescue! import os import MySQLdb files = os.listdir("d:\\hedge\\old data\\") conn = MySQLdb.connect(host = 'host', user = 'user', passwd = 'pass', db = 'db') curs = conn.cursor() for line in files: if line[-4:] == "2006": SQL = "LOAD DATA " SQL += "INFILE 'd:/hedge/old data/" + line SQL += "' INTO TABLE iv " SQL += "FIELDS TERMINATED BY ',' " SQL += "(symbol, date, iv, pv, cv);" curs.execute(SQL) time to execute = 5 mins invective dispensed while executing = 0 A hearty thank you and Happy Holidays to all in the Python community. jab -- http://mail.python.org/mailman/listinfo/python-list
Fuzzy Lookups
I have some CDs and have been archiving them on a PC. I wrote a Python script that spans the archive and returns a list of its contents: [[genre, artist, album, song]...]. I wanted to add a search function to locate all the versions of a particular song. This is harder than you might think. For example the Cajun "national anthem" is Jolie Blond, but it can be spelled several different ways jolie, joli, blon, blond, etc... In addition the various online services that provide song info are riddled with typos, so an ordinary string match just doesn't get you there. What is needed is a fuzzy string match and it turns out that there is a very good one, the Levenshtein distance, which is the number of inserts, deletions and substitutions needed to morph one string into another. In my application I match the desired song title against all song titles in my list and return the ones with the lowest Levenshtein distances. This is remarkably, one might even say stunningly, effective, easily finding all the version of Jolie Blon in the list. I am using the following snippet (found on the web, proper attribution unsure), which calculates the Levenshtein distance. def distance(a,b): c = {} n = len(a); m = len(b) for i in range(0,n+1): c[i,0] = i for j in range(0,m+1): c[0,j] = j for i in range(1,n+1): for j in range(1,m+1): x = c[i-1,j]+1 y = c[i,j-1]+1 if a[i-1] == b[j-1]: z = c[i-1,j-1] else: z = c[i-1,j-1]+1 c[i,j] = min(x,y,z) return c[n,m] As mentioned above this works quite well and I am happy with it, but I wonder if there is a more Pythonic way of doing this type of lookup? jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy Lookups
Diez B. Roggisch wrote: > I did a levenshtein-fuzzy-search myself, however I enhanced my version by > normalizing the distance the following way: Thanks for the snippet. I agree that normalizing is important. A distance of three is one thing when your strings are long, but quite another when they are short. I'd been thinking about something along these lines myself, but hadn't gotten there yet. It'll be interesting to have a look at the distribution of the normalized numbers, I'd guess that there may be a rough threshold that effectively separates the wheat from the chaff. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy Lookups
Diez B. Roggisch wrote: > I did a levenshtein-fuzzy-search myself, however I enhanced my version by > normalizing the distance the following way: > > def relative(a, b): > """ > Computes a relative distance between two strings. Its in the range > (0-1] where 1 means total equality. > @type a: string > @param a: arg one Hello, I adapted your approach to my needs and thought you might like to see the result def LevenshteinRelative(a, b): """ Returns the Levenshtein distance between two strings as a relative quantity in the range 1 to 0 where 1.0 is a perfect match. """ # Calculate the Levenshtein distance. (returns an integer) dist = LevenshteinDistance(a, b) # dist is at most the length of the longer string. max_dist = float(max(len(a), len(b))) # dist is always at least the difference of the sizes of the two strings. min_dist = max_dist - float(min(len(a), len(b))) try: # If max_dist and min_dist are equal use simple form. relative = 1.0 - (dist - min_dist) / (max_dist - min_dist) except ZeroDivisionError: relative = 1.0 - dist / max_dist return relative Thanks, jab -- http://mail.python.org/mailman/listinfo/python-list
with as a reserved word
I gather that 'with' is on its way to becoming a reserved word. Is this something that will break? import Gnuplot gp = Gnuplot.Gnuplot(debug=1) data = Gnuplot.Data([1,2,3,4,3,2,3,4,3,2,1], with='linespoints') gp.plot(data) >>> :3: Warning: 'with' will become a reserved keyword in Python 2.6 http://www.gnuplot.info/ http://gnuplot-py.sourceforge.net/ This was run by PyScripter 1.8.7.1 with Python 2.5. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: with as a reserved word
On Jun 11, 11:34 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > if you have Python 2.5, you can try it out yourself: > > >>> dict(with=1) > :1: Warning: 'with' will become a reserved keyword in Python 2.6 > {'with': 1} > > >>> from __future__ import with_statement > >>> dict(with=1) >File "", line 1 > dict(with=1) > ^ > SyntaxError: invalid syntax I see that this change appears to be final. http://www.python.org/dev/peps/pep-0343/ I don't have an opinion, pro or con, on this PEP, but I'll bet that it breaks a lot of code. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: with as a reserved word
On Jun 11, 12:47 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > estimating what "a lot" is isn't trivial, but it's worth noting that a > search for "lang:python \swith\W" over at google's code search only > brings up about 200 cases, and most of those are found in comments and > string literals. and in Zope. Well then, it would seem that the problems will be minimal. Good. I'll have a look a the gnuplot.py code and post heads-up note to their list. Thanks, jab -- http://mail.python.org/mailman/listinfo/python-list
name space problem
An example: class classA: def __init__(self): self.b = 1 def doStuff(): some calcs a..b = 0 a = classA(): print a.b doStuff() print a.b That works as hoped, printing 1, 0. But, if I move doStuff to another module and: import doStuff class classA: def __init__(self): self.b = 1 a = classA() print a.b doStuff.doStuff() print a.b I get a 1 printed and an error: NameError: global name 'a' is not defined I think this is a name space issue, but I can't grok it. Thanks in advance, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: name space problem
On Oct 23, 4:20 pm, [EMAIL PROTECTED] wrote: > Hello. Indeed the doStuff function in the doStuff module can't do 'a.b > = 0' (the double dot was just a typo, right?) Yes. > because it doesn't know anything about an object named a. I was trying to understand why it worked when written in, but not when included. > I think the right solution would be not to use 'a' as a global > variable, but rather to pass it as an explicit parameter to the > function. Does doing this make a copy of a? > In module doStuff: > > def doStuff(a): > # some calcs > a.b = 0 > > In the main module: > > import doStuff > # ... > doStuff.doStuff(a) In my real ap a is quite large... thanks, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: name space problem
Thank you. jab -- http://mail.python.org/mailman/listinfo/python-list
time series data and NumPy
Good morning, I store time series data in a SQL database. The results of a typical query using pyodbc look like this. DateClose "2007-01-17" 22.57 Where Date is a datetime.date object and Close is a float. I'd like to put this data in a NumPy array for processing, but am unsure as to how to handle the date. In the past I've used lists, but I am looking to boost speed a bit as I wish to do a large number of transformations and comparisons. Can one index an array using datetime objects? For example it would be nice to do a union of two arrays so that any dates missing in either one were eliminated. Thoughts on doing rolling operations, such as an n-period average or variance? Thoughts on working with time series data in arrays in general? Thanks in advance, jab--who is very happily returning to Python after a sojourn in R-land -- http://mail.python.org/mailman/listinfo/python-list
Re: time series data and NumPy
On Jan 26, 9:29 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > What you could do would be to convert the date-column into a timestamp, > which is a int/long, and use that. Would that help? Actually that might help, as all I need the date for is to index values. Thanks, I'll give it a spin. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: time series data and NumPy
On Jan 26, 10:18 am, Bob Greschke wrote: > You're using the Python-MySQL module mysqldb, right? Actually I using MySQL with pyodbc as the mysqldb Windows binaries for Python 2.5 aren't out yet. :-( > You can select the data from the database and have > MySQL do the conversion with an SQL command. > > select to_days(), > from ; That works very nicely indeed. Thanks, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: time series data and NumPy
On Jan 26, 10:46 am, Robert Kern <[EMAIL PROTECTED]> wrote: > Yes, one can make numpy arrays with "object" as its type. One can even extend > the C-level parts as well. For example, we have an experimental package in the > scipy sandbox for uniform time series that uses mx.DateTime. > > http://www.scipy.org/TimeSeriesPackage I saw that, but I am working with irregular time series. > This is frequently what I do. For dates, I like Modified Julian Day Numbers > although I am sure that would horrify some people more knowledgeable than I. Not horrified, just doing it. ;-) jab -- http://mail.python.org/mailman/listinfo/python-list
Python editor
No, no, no, this is not an invitation to the editor wars. I have been using José Cláudio Faria's superb Tinn-R, http://www.sciviews.org/Tinn-R/, with the R language, http://www.r-project.org/. This editor allows you to send code to the R shell for execution. You can easily send a line, the selection, the balance after the cursor or the whole script. I have recently decided to move this project to Python instead of R. However, I miss the interaction with the shell a la Tinn-R. Do any of the Python editors support this feature? I would be especially interested in using IPython as the shell. jab -- http://mail.python.org/mailman/listinfo/python-list
Tokens?
In the invaluable 'Dr. Dobb's Python-URL! - weekly Python news and links' of April 17 Peter Otten writes: "Michele Simionato's little script lets you search for a name in Python scripts, avoiding false positives that a standard tool like grep would yield." Can someone explain why this is so? I have attached the script below. import sys, tokenize, token def count_name(name, script): "Count the occurrences of a Python name in a script" counter = 0 for tok_code, tok_value, (srow, scol), (erow, ecol), line in \ tokenize.generate_tokens(file(script).readline): if tok_code == token.NAME and tok_value == name: counter += 1 print 'line %s: %s' %(srow, line), if counter: print '*** %s ***\n' % script return counter if __name__ == '__main__': name = sys.argv[1] scripts = sys.argv[2:] total = sum(count_name(name, script) for script in scripts) print 'Found %d occurrences of %r' % (total, name) jab--who laments the day that Doctor Dobbs' Journal of Computer Callisthenics and Orthodontics changed its name. -- http://mail.python.org/mailman/listinfo/python-list
list*list
There must be a better way to multiply the elements of one list by another: a = [1,2,3] b = [1,2,3] c = [] for i in range(len(a)): c.append(a[i]*b[i]) a = c print a [1, 4, 9] Perhaps a list comprehension or is this better addressed by NumPy? Thanks, jab -- http://mail.python.org/mailman/listinfo/python-list
Re: list*list
Very useful comments... Thanks to all! Once again this community has demonstrated why Python is THE language. jab -- http://mail.python.org/mailman/listinfo/python-list
Re: A different kind of interface
PyScripter does it for me. http://code.google.com/p/pyscripter/ jab -- http://mail.python.org/mailman/listinfo/python-list