Python Remote Work
Hi, Bit of a long shot! I am looking for remote work and like the look of Python. I am an Australian, with a degree in Commercial computing, with 15 years using Delphi in a business environment. I am looking for a company to X train me to Python remotely, or maybe even a remote Python developer with too much work on his/her plate that wants some paid help and is willing to point me in the right direction. cheers Shane -- https://mail.python.org/mailman/listinfo/python-list
repeating regular expressions in one string
Hi folks,
I'm new to regular expressions (and a novice at Python) but it seems to be the
tool I need for a particular problem. I have a bunch of strings that looks like
this:
'blahblah_sf1234-sf1238_blahblah'
and I would like to use the re module to parse all the 'sf' parts of the
string. Each 'sf' needs to be its own string when I am through. How do I
compile a regular expression that looks for more than one instance? Currently
my expression looks like this:
myString = re.compile('sf[0-9][0-9][0-9][0-9]')
This works great for finding the first instance of 'sf'. I hope that was
clear :)
Thanks,
Shane
--
http://mail.python.org/mailman/listinfo/python-list
searching for files on Windows with Python
I've been giving Google a good workout with no luck. I would like to be able to search a Windows filesystem for filenames, returning a list off absolute paths to the found files, something like: def findFiles(filename, pathToSearch): ... ... return foundFileNames Is the os module where I should start? Thanks, Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Python book for a non-programmer
Simon Brunning wrote: > I wouldn't have thought either of those was suitable for a > non-programmer. Great for cross-trainers, yes, but neither is intended > as a programming tutorial. I agree, I just thought that the other replies had provided more than enough resources to cover the basics, so I was just suggesting some material that could be used when the basics had been absorbed. Sorry about the confusion. -- http://mail.python.org/mailman/listinfo/python-list
re: type(), modules: clarification please
Hi,
When one writes,
> className='Employee'
> baseClasses = ...
> dictionary = { ... }
> newClass = type( className, , dictionary)
in what module does newClass belong? If it's the current module what
code
do I run to print out the name of that module in a.b.c... form?
Related: If I want to make `newClass' into a module called, say,
a.b.c.d.e
how do I do that? This doesn't work:
> className='a.b.c.d.e.Employee'
> newClass = type( className, , dictionary)
As far as I know the correct procedure involves three steps:
> import a.b.c.d.e as targetModule
> newClass = type( className, , dictionary)
> setattr( targetModule, 'newClassInTarget', newClass )
> obj = targetModule.newClassInTarget()
I am not sure if newClass remains a valid type in whatever module it
was
created in.
Final question. If, as an academic exercise I wanted to recursively
dump
all the classes in a module (and its sub-modules) would I do this? [I
use
some pseudo code]:
def dump( namespace ):
for i in dir(namespace):
if i is a class:
print i
elif i is a module:
dump(i)
dump( )
Thank you
--
http://mail.python.org/mailman/listinfo/python-list
type vs. module (part2)
In the following t,t1 are the result of built-in call type() -- the form that takes three arguments. Therefore they are classes. Consider the following output: print type(t) > print id(t) >1234567 print t.__module__ >a.b.t.d print type(t1) > print id(t1) >1234568 print t1.__module__ >a.b.t.d I now have two questions: How does Python allow two classes of the same type as evidenced by identical ``print type()' output and different id outputs? Also, which module is t,t1 actually in? Is it "a.b.f"? Or is it "a.b.t.d". I am totally confused. -- http://mail.python.org/mailman/listinfo/python-list
define module in non-standard location?
Normally if one has a code set under a directory "top_level" like this: top_level: __main__.py a __init__.py b __init__.py then this directory structure is naturally satisfies this line in __main__.py: >import a.b But support, for some stupid reason --- say a.b is user defined code --- that I want to locate modules a and a.b somewhere else under another directory "other_top_level". What would the line "import a.b" in __main__,py be replaced by? Thank you -- http://mail.python.org/mailman/listinfo/python-list
Re: define module in non-standard location?
On Oct 16, 11:55 pm, Steven D'Aprano wrote: > On Sun, 16 Oct 2011 19:43:20 -0700, Shane wrote: > > Normally if one has a code set under a directory "top_level" like this: > > > top_level: > > __main__.py > > a > > __init__.py > > b > > __init__.py > > > then this directory structure is naturally satisfies this line in > > __main__.py: > > >>import a.b > > > But support, for some stupid reason --- say a.b is user defined code --- > > that I want > > to locate modules a and a.b somewhere else under another directory > > "other_top_level". > > You mean like this? > > top_level/ > __main__.py > other_top_level/ > a/ > __init__.py > b/ > __init__.py > > > What would the line "import a.b" in __main__,py be replaced by? > > Make sure other_top_level is in your PYTHONPATH, and then just use > "import a.b" as before. > > Either use your shell to do something like this: > > export PYTHONPATH=other_top_level:$PYTHONPATH > > (that's using bash syntax, other shells may need something different), or > in __main__.py do this: > > import sys > sys.path.append(other_top_level) > > -- > Steven Cheers. Thanks -- http://mail.python.org/mailman/listinfo/python-list
non-standard module location (again)
Need to refine a question I asked earlier. If I have a module, |-- foo |---| |---|---bar |---|---| |---|---|---__init__.py then I can say import foo.bar But suppose I want to import foo.bar.stuff and stuff isn't located on under `bar' because it's user supplied code: |- stuff <- I want this to be logically under foo.bar |---|---__init__.py <- even though it's not under bar in the computer's file system Now what: how to arrange to do command: import foo.bar.stuff -- http://mail.python.org/mailman/listinfo/python-list
parallel and/or synchronous start/run/stop on multiple boxes
Consider a network of 3 fully-connected boxes i.e. every box as a TCP- IP connection to every other box. Suppose you start a python program P on box A. Is there a Python mechanism for P to send a copy of itself to box B or C then start that program P on B or C by running a method p in P? Is there a way that P on A could wait for that result? Bottom line: we have a number of needs in which it'd very convienent to write a phython program, install and run it, so that it could automatically propogate itself to wherever the code needs to go and run itself so that there could be multiple copies of the code running on different boxes with synchrounous (maybe asychronous) communication. Shane -- http://mail.python.org/mailman/listinfo/python-list
How to dynamic insert more conditional statements into a function
Hi I am a newbie for Python Here is a question, say I have a list L, function foo is: def foo(L): if L[0] > 0: return True if later I want another function goo which returns "True" when L[0] and L[1] are both > 0, i.e., def goo(L): if L[0] > 0 and L[1] > 0: return True Can anybody tell me how can I write the function "goo" based upon the function "foo"? Thanks! I know if it is implementable, I should use "decorator". But I just cannot figure out how. -- http://mail.python.org/mailman/listinfo/python-list
Some objects missing from tkinter
I'm new to Python, so I'll try to be clear about my problem. I'm using Python 3.1 (latest stable version from python.org) on Windows 7. I have a program using tkinter for UI, and it works properly from both pything GUI shell, and running from command prompt, EXCEPT that I have a menu command to invoke tkinter.filedialog.askopenfile, and it fails because it says: file = tkinter.filedialog.askopenfilename() AttributeError: 'module' object has no attribute 'filedialog' I made a simple test program: import tkinter print (dir(tkinter)) when I run this from the GUI shell, the results include filedialog, but from the command prompt, it does not (also missing other attributes, such as messagebox). All the UI widgets work properly. My best hypothesis at this point is that from the GUI shell its using the source code under lib\tkinter (where there is a filedialog.py), but from the command shell it is using the compiled dll, and that doesn't export filedialog for some reason. Thanks in advance for any suggestions -- http://mail.python.org/mailman/listinfo/python-list
Re: Some objects missing from tkinter
On Apr 26, 11:58 am, Lie Ryan wrote: > On 04/27/10 03:50, Peter Otten wrote: > > > It is a bit unfortunate that your editor has side effects on your program, > > and I recommend that you never trust the result of importing a module from > > within idle's shell completely. > > In fact, never trust IDLE. IDLE is a nice IDE when the alternative is > Notepad; but for serious work, you need a real IDE or a programmer's > text editor (vim or emacs, whichever side you're in). > > Always test the you write inside IDLE on a command line. Thank you both for the replies. importing tkinter.filedialog did the trick, and I appreciate the advice about not depending on IDLE. I'm not planning on doing any serious work, so I will probably continue to rely on IDLE for now. -- http://mail.python.org/mailman/listinfo/python-list
Python site-packages permission denied?
What do I need to do to successfully install a package onto python so that I can use it as a module? I have tried in terminal in the correct directory "python2.7 ./setup.py install" but it says permission denied. Shanes-MacBook-Pro:seisen-urllib2_file-cf4c4c8 chimpsarehungry$ python2.7.1 ./setup.py install -bash: python2.7.1: command not found Shanes-MacBook-Pro:seisen-urllib2_file-cf4c4c8 chimpsarehungry$ python ./setup.py install running install running build running build_py running install_lib copying build/lib/urllib2_file.py -> /Library/Python/2.7/site-packages error: /Library/Python/2.7/site-packages/urllib2_file.py: Permission denied Shanes-MacBook-Pro:seisen-urllib2_file-cf4c4c8 chimpsarehungry$ -- http://mail.python.org/mailman/listinfo/python-list
Help with MultipartPostHandler
Hi Python Google Group! I hope someone could help me and then one day when I am
good I can contribute to the forum as well. Does anyone know what is wrong with
my syntax here as I am trying to submit this form using MultipartPostHandler
that I installed?
import MultipartPostHandler, urllib2
params = { 'Run_Number' : 'NONE', 'MAX_FILE_SIZE' : '200', 'submitForm' :
'Submit' }
opener.open("http://consurf.tau.ac.il/index_full_form_PROT.php";, params)
--
http://mail.python.org/mailman/listinfo/python-list
Remote Work Wanted - Junior Python Developer
Hi, I would like to start working remotely programming in Python. I have an IT Degree and English is my first Language. I currently live in New Zealand, thus the remote work is the only option. I have 15+ years experience as a Delphi Developer, mainly working with business database applications. I have worked with Oracle, MySQL, SQL Server, Interbase, Firebird and many other database backends as well. I would like to join a team of developers that could help my crossover to Python. I think I can hit the ground running if pointed in the right direction and helped over any time consuming hurdles. As far as money goes, I am open to reasonable offers. cheers Shane -- https://mail.python.org/mailman/listinfo/python-list
Re: A Moronicity of Guido van Rossum
Delaney, Timothy (Tim) wrote: > You have to admit though, he's remarkably good at getting past > Spambayes. Despite classifying *every* Xah Lee post as spam, he still > manages to get most of his posts classified as 0% or 1% spam. Hmm, perhaps he's using steganography. Maybe the emails actually contain an encoded message for an anonymous recipient, and he writes stuff designed to get past filters. If I were a spy, I might do the same thing... but I'd use language less likely to get me booted from the list. ;-) Shane -- http://mail.python.org/mailman/listinfo/python-list
How to get listed on planet.python.org
I've been writing Python-related articles on my weblog, and I thought planet.python.org might be interested in including them. Does anyone know how to submit a feed to the aggregator? http://hathawaymix.org/Weblog/rss20.xml?categories:list=Python Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Python book for a non-programmer
http://www.python.org/doc/Intros.html and two great texts when she has covered the basics are: http://diveintopython.org/ http://www.mindview.net/Books/TIPython -- http://mail.python.org/mailman/listinfo/python-list
Re: PythonMagick on Windows
Adam Endicott wrote: > Claudio Grondi wrote: > >>Why is PIL *(Python Image Library) not suitable to do the graphics? >> >>http://www.pythonware.com/products/pil/ >> >>Claudio > > > I am using PIL for some other things, but what I need ImageMagick for > is to convert a PDF into a series of jpegs (one per page). Its the only > tool I've found to do this at a good enough resolution. FWIW: I tried PythonMagick for a while, but soon found that large or complex images caused it to consume excessive memory and sometimes crash, taking my application with it. I switched to the command line interface to isolate my application from problems in Graphics/ImageMagick. Everything ended up much more robust that way, and the time cost is small--it's on the order of milliseconds. Shane -- http://mail.python.org/mailman/listinfo/python-list
Proposal: Inline Import
Here's a heretical idea.
I'd like a way to import modules at the point where I need the
functionality, rather than remember to import ahead of time. This might
eliminate a step in my coding process. Currently, my process is I
change code and later scan my changes to make matching changes to the
import statements. The scan step is error prone and time consuming.
By importing inline, I'd be able to change code without the extra scan step.
Furthermore, I propose that the syntax for importing inline should be an
expression containing a dot followed by an optionally dotted name. For
example:
name_expr = .re.compile('[a-zA-Z]+')
The expression on the right causes "re.compile" to be imported before
calling the compile function. It is similar to:
from re import compile as __hidden_re_compile
name_expr = __hidden_re_compile('[a-zA-Z]+')
The example expression can be present in any module, regardless of
whether the module imports the "re" module or assigns a different
meaning to the names "re" or "compile".
I also propose that inline import expressions should have no effect on
local or global namespaces, nor should inline import be affected by
local or global namespaces. If users want to affect a namespace, they
must do so with additional syntax that explicitly assigns a name, such as:
compile = .re.compile
In the interest of catching errors early, it would be useful for the
Python parser to produce byte code that performs the actual import upon
loading modules containing inline import expressions. This would catch
misspelled module names early. If the module also caches the imported
names in a dictionary, there would be no speed penalty for importing
inline rather than importing at the top of the module.
I believe this could help many aspects of the language:
- The coding workflow will improve, as I mentioned.
- Code will become more self-contained. Self-contained code is easier
to move around or post as a recipe.
- There will be less desire for new builtins, since modules will be just
as accessible as builtins.
Thoughts?
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Benji York wrote: > Why not: 1) jump to the top of the file when you need to do an import > (1G in Vim), 2) add the import, 3) jump back to where you were (Ctrl-o > in Vim) and keep coding. This isn't Vim specific, I suspect all decent > editors have similar capabilities (I know Emacs does). Thus eliminating > the unpleasant scan step. That's something the computer should do for me. It's busywork. Eclipse practically eliminates this busywork when I'm writing Java code: if I autocomplete a name, it also quietly adds the corresponding import statement. It also generates import statements when I copy/paste code. The structure of the Java language makes this relatively easy. In Python, it's harder to do autocompletion with the level of accuracy required for import statement generation. However, inline imports could eliminate the need to generate import statements. > Oh, and py.std does something similar to what you want: > http://codespeak.net/py/current/doc/misc.html#the-py-std-hook. Either form of inline import (py.std or my proposal) is a major idiomatic change. I can't be too much of a cowboy and start using idioms that are completely different from standard Python usage; my code would become unmaintainable. Thus a prerequisite for using inline import is broad approval. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Benji York wrote: > OK, good. You won't have to worry about that. :) You didn't give a reason for disliking it. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Xavier Morel wrote:
> Shane Hathaway wrote:
>
>>Thoughts?
>
>
> >>> import re; name_expr = re.compile('[a-zA-Z]+')
> >>> name_expr
> <_sre.SRE_Pattern object at 0x00F9D338>
> >>>
>
> the import statement can be called anywhere in the code, why would you
> add strange syntactic sugar that doesn't actually bring anything?
That syntax is verbose and avoided by most coders because of the speed
penalty. It doesn't replace the idiom of importing everything at the
top of the module.
What's really got me down is the level of effort required to move code
between modules. After I cut 100 lines from a 500 line module and paste
them to a different 500 line module, I have to examine every import in
both modules as well as examine the code I moved for missing imports.
And I still miss a lot of cases. My test suite catches a lot of the
mistakes, but it can't catch everything.
If I could just avoid import statements altogether, moving code would be
easier, regardless of extra typing. But I can't avoid import statements
unless there's a different way to import that lots of people like.
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Mike Meyer wrote: > Shane Hathaway <[EMAIL PROTECTED]> writes: >>I'd like a way to import modules at the point where I need the >>functionality, rather than remember to import ahead of time. This >>might eliminate a step in my coding process. Currently, my process is >>I change code and later scan my changes to make matching changes to >>the import statements. The scan step is error prone and time >>consuming. By importing inline, I'd be able to change code without the >>extra scan step. > > > As others have pointed out, you can fix your process. That your > process doesn't work well with Python isn't a good reason for changing > Python. Do you have any ideas on how to improve the process of maintaining imports? Benji's suggestion of jumping around doesn't work for moving code and it interrupts my train of thought. Sprinkling the code with import statements causes a speed penalty and a lot of clutter. I'm actually quite surprised that others aren't bothered by the process of maintaining imports. Perhaps the group hasn't spent time in Eclipse to see what a relief it is to have imports managed for you. The difference isn't enough to make anyone jump ship to Java, but it's a real improvement. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Mike Meyer wrote: > Shane Hathaway <[EMAIL PROTECTED]> writes: >>That syntax is verbose and avoided by most coders because of the speed >>penalty. > > > What speed penalty? "import re" is a cheap operation, every time but > the first one in a program. I'm talking about using imports *everywhere*. The penalty would be appreciable. >>What's really got me down is the level of effort required to move code >>between modules. After I cut 100 lines from a 500 line module and >>paste them to a different 500 line module, I have to examine every >>import in both modules as well as examine the code I moved for missing >>imports. > > > Has it ever occured to you that if you're cutting and pasting 500 line > blocks, you're doing something fundamentally wrong? One of the points > of modules and OO is that you don't *have* to do things like > that. Cut-n-paste means you wind up with two copies of the code to > maintain, so that bug fixes in one will have to be propogated to the > other "by hand". Rather than spend time fixing what you broke by > yanking the code out of it's context, you'd be better off refactoring > the code so you could use it in context. That'll cut down on the > maintenance in the future, and may well mean that the next time > someone needs the code, it'll already be properly refactored so they > can use it directly, without having to cut-n-paste-n-fix it again. I said cut and paste, not copy and paste. I'm moving code, not copying it. Your advice is correct but doesn't apply to this problem. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Kent Johnson wrote: > Shane Hathaway wrote: >>I'm talking about using imports *everywhere*. The penalty would be >>appreciable. > > > Have you tried it? > > D:\Projects\CB>python -m timeit -s "import re" "import re" > 100 loops, best of 3: 1.36 usec per loop > > You need a lot of imports before 1 usec becomes "appreciable". Let me fully elaborate the heresy I'm suggesting: I am talking about inline imports on every other line of code. The obvious implementation would drop performance by a double digit percentage. > And your > proposal is doing the import anyway, just under the hood. How will you > avoid the same penalty? The more complex implementation, which I suggested in the first message, is to maintain a per-module dictionary of imported objects (distinct from the global namespace.) This would make inline imports have almost exactly the same runtime cost as a global namespace lookup. But never mind, this proposal is a distraction from the real issue. See the next thread I'm starting. Shane -- http://mail.python.org/mailman/listinfo/python-list
Managing import statements
Let's talk about the problem I really want help with. I brought up a proposal earlier, but it was only half serious. I realize Python is too sacred to accept such a heretical change. ;-) Here's the real problem: maintaining import statements when moving sizable blocks of code between modules is hairy and error prone. I move major code sections almost every day. I'm constantly restructuring the code to make it clearer and simpler, to minimize duplication, and to meet new requirements. To give you an idea of the size I'm talking about, just today I moved around 600 lines between about 8 modules, resulting in a 1400 line diff. It wasn't just cut-n-paste, either: nearly every line I moved needed adjustment to work in its new context. While moving and adjusting the code, I also adjusted the import statements. When I finished, I ran the test suite, and sure enough, I had missed some imports. While the test suite helps a lot, it's prohibitively difficult to cover all code in the test suite, and I had lingering doubts about the correctness of all those import statements. So I examined them some more and found a couple more mistakes. Altogether I estimate I spent 20% of my time just examining and fixing import statements, and who knows what other imports I missed. I'm surprised this problem isn't more familiar to the group. Perhaps some thought I was asking a newbie question. I'm definitely a newbie in the sum of human knowledge, but at least I've learned some tiny fraction of it that includes Python, DRY, test-first methodology, OOP, design patterns, XP, and other things that are commonly understood by this group. Let's move beyond that. I'm looking for ways to gain just a little more productivity, and improving the process of managing imports could be low-hanging fruit. So, how about PyDev? Does it generate import statements for you? I've never succeeded in configuring PyDev to perform autocompletion, but if someone can say it's worth the effort, I'd be willing to spend time debugging my PyDev configuration. How about PyLint / PyChecker? Can I configure one of them to tell me only about missing / extra imports? Last time I used one of those tools, it spewed excessively pedantic warnings. Should I reconsider? Is there a tool that simply scans each module and updates the import statements, subject to my approval? Maybe someone has worked on this, but I haven't found the right Google incantation to discover it. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Steve Holden wrote: > Shane Hathaway wrote: >> The structure of the Java language makes this relatively easy. >> > > And there's so much more busywork in Java that it's probably worth > automating. See > >http://www.artima.com/weblogs/viewpost.jsp?thread=42242 Agreed. Here's my take on Java vs. Python: http://hathawaymix.org/Weblog/2004-06-16 Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Managing import statements
Jean-Paul Calderone wrote: > On Sat, 10 Dec 2005 02:21:39 -0700, Shane Hathaway <[EMAIL PROTECTED]> wrote: >>How about PyLint / PyChecker? Can I configure one of them to tell me >>only about missing / extra imports? Last time I used one of those >>tools, it spewed excessively pedantic warnings. Should I reconsider? > > > I use pyflakes for this: <http://divmod.org/trac/wiki/DivmodPyflakes>. The > *only* things it tells me about are modules that are imported but never used > and names that are used but not defined. It's false positive rate is > something like 1 in 10,000. That's definitely a good lead. Thanks. > This is something I've long wanted to add to pyflakes (or as another feature > of pyflakes/emacs integration). Is there a community around pyflakes? If I wanted to contribute to it, could I? Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Managing import statements
Chris Mellon wrote: > On 12/10/05, Shane Hathaway <[EMAIL PROTECTED]> wrote: >>I'm surprised this problem isn't more familiar to the group. Perhaps >>some thought I was asking a newbie question. I'm definitely a newbie in >>the sum of human knowledge, but at least I've learned some tiny fraction >>of it that includes Python, DRY, test-first methodology, OOP, design >>patterns, XP, and other things that are commonly understood by this >>group. Let's move beyond that. I'm looking for ways to gain just a >>little more productivity, and improving the process of managing imports >>could be low-hanging fruit. > > It is probably because most people don't regularly switch that much > code around, or use that many modules. I think the fact that you move > that much code between modules is probably a code smell in and of > itself - you're clearly moving and changing responsibilities. > Refactoring in smaller chunks, less extreme refactoring, correcting > the imports as you go, and avoiding inline imports without a really > good reason will probably help you a lot more than a new syntax. That's an insightful suggestion, but I'm pretty sure I'm doing it right. :-) I fill my mind with the way the code should look and behave when it's done, then I go through the code and change everything that doesn't match the picture. As the code moves closer to production and bugs become more expensive, I add a step to the process where I perform more formal refactoring, but that process takes much longer. The problems caused by the informal process are less expensive than the formal process. > Yes. Spend an afternoon looking at PyLints options, get rid of the > stuff you don't like, and use it regularly. PyDev has PyLint > integration, which is nice. Ok, thanks. > I don't think I've ever imported more than a dozen modules into any > given file. I rarely find it neccesary to move huge chunks of code > between my modules - the biggest such I did was when I moved from a > single-file proof of concept to a real modular structure, and that > only took me an hour or so. If I'd done it modularly to start with it > would have been fine. Well, code moves a lot in the immature phases of large applications like Zope and distributed systems. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop until condition is true
Remi Villatel wrote: > Hi there, > > There is always a "nice" way to do things in Python but this time I can't > find one. > > What I'm trying to achieve is a conditionnal loop of which the condition > test would be done at the end so the loop is executed at least once. It's > some way the opposite of "while". > > So far, all I got is: > > while True: > some(code) > if final_condition is True: > break > # > # > > What I don't find so "nice" is to have to build an infinite loop only to > break it. FWIW, my own experience is that the "while True" idiom is actually safer and better than alternatives like do/while. I used to write do/while loops all the time, but I wound up with more than my fair share of unintentionally infinite loops. I put too much trust in the syntax: I expected that since I was using the cleaner construct, I didn't have to worry about infinite loops. Now, I think of "while True" not as an infinite loop, but rather as a sign reminding me to be wary of looping infinitely in a particular spot. I feel like this has resulted in a lot fewer infinite loops in my own code. Now I believe that any loop that can't be represented well with "for" or a conditional "while" has enough inherent complexity to justify a warning sign, and "while True" has bright yellow flashing lights all over it. Thus I'm quite in favor of the status quo. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: timeoutsocket.py mirror?
[EMAIL PROTECTED] wrote: > Can anybody provide a mirror for this module? The original URL seems to > be down. I have been looking for over an hour, and every single > resource that uses it links to the timo-tasi.org site to download it. FWIW, Python 2.3+ has the timeout functionality built in, so timeoutsocket.py is no longer needed. See the documentation on socket.Socket.settimeout(). Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: time.clock() or time.time()
[EMAIL PROTECTED] wrote: > What's the difference between time.clock() and time.time() > (and please don't say clock() is the CPU clock and time() is the actual > time because that doesn't help me at all :) > > I'm trying to benchmark some function calls for Zope project and when I > use t0=time.clock(); foo(); print time.clock()-t0 > I get much smaller values than when I use time.clock() (most of them > 0.0 but some 0.01) > When I use time.time() I get values like 0.0133562088013, > 0.00669002532959 etc. > To me it looks like time.time() gives a better measure (at least from a > statistical practical point of view). time.time() is very likely what you want. time.time() measures real time, while time.clock() measures the time the CPU dedicates to your program. For example, if your function spends 2 seconds doing pure I/O and 3 seconds doing pure computation, you should expect time.time() to show 5 that seconds elapsed, and time.clock() to show that the CPU was consumed by your program for 3 seconds. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: time.clock() or time.time()
Magnus Lycka wrote: > Shane Hathaway wrote: > >>time.time() measures real time, while time.clock() measures the time the >>CPU dedicates to your program. > > > I suppose that varies with the platform... "help(time.clock)" says: > > Help on built-in function clock: > > clock(...) > clock() -> floating point number > > Return the CPU time or real time since the start of the process or > since > the first call to clock(). This has as much precision as the > system records. > > Another thing to notice is that depending on OS, either time.time() or > time.clock() might have much higher precision than the other. I didn't notice that. Thanks. However, isn't this thoroughly un-Pythonic? No wonder people have to ask. Wouldn't it be better to have: time.time() -> real time, with as much precision as the platform provides. Does not wrap around. time.cputime() -> CPU time, or real time on platforms that don't measure CPU time separately from real time. May wrap around in long-running processes. Shane -- http://mail.python.org/mailman/listinfo/python-list
urllib versus IIS
I started experimenting with SOAPpy yesterday and immediately hit a snag. Both web services I tried simply hung and never replied. After a lot of digging, I found out what was going wrong: urllib.urlopen() is issuing an HTTP/1.0 request, but Microsoft IIS 5 ignores the client HTTP version and replies with an HTTP/1.1 response. This is a problem because while HTTP/1.0 servers are expected to close the connection once the response is finished, HTTP/1.1 servers are allowed keep the connection open. urllib assumes that the connection always closes. Therefore, when urllib receives an HTTP/1.1 response, it hangs until the server feels inclined to close the connection. Obviously the server is wrong, since HTTP/1.0 requests should only receive HTTP/1.0 responses, but I can't do anything about that. Now, there is actually code in httplib that would allow urllib to correctly understand HTTP/1.1 responses, if only urllib used it. After the headers have been parsed, urllib calls getfile(), but I think it should call getresponse() instead. The result of getresponse() is almost like a file; it just needs readline(s) and iteration. In fact, perhaps httplib's getfile() should be deprecated, since HTTP/1.1 has several options for encoding the response body (chunking and compression) and users of httplib shouldn't have to know about those encodings. Users should use getresponse() instead. So, is it worth my time to fix urllib and httplib, making urllib use getresponse() instead of getfile()? Would the changes be accepted? Is anyone else working on something similar? BTW, this is on Python 2.3.5, but I haven't spotted any changes between Python 2.3.5 and current CVS that would have fixed the problem. I'll start trying Python CVS in a moment. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: pre-PEP: Suite-Based Keywords
Kent Johnson wrote: > Brian Sabbey wrote: >> Using suite-based keyword arguments, the code >> >> f(x = 1) >> >> is equivalent to >> >> f(): >>x = 1 > > > ISTM the syntax is ambiguous. How do you interpret > if f(): > x = 1 > ? > > Is a suite alllowed only when a block could not be introduced in the > current syntax? I like this PEP a lot, but your concern is valid. Maybe Brian could modify the PEP slightly to disambiguate. How about using an ellipsis in the argument list to signify suite-based keywords? Examples: f(...): x = 1 class C(object): x = property(...): doc = "I'm the 'x' property." def fget(self): return self.__x def fset(self, value): self.__x = value def fdel(self): del self.__x d = dict(...): a = 1 b = 2 Using an ellipsis in a statement that would begin a different kind of block is illegal and generates a syntax error. Note that this usage seems to fit well with the definition of "ellipsis". http://dictionary.reference.com/search?q=ellipsis Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: pre-PEP: Suite-Based Keywords
Brian Sabbey wrote:
> Maybe using '**' would be better than '...' since it already is used to
> indicate keyword arguments. Example:
>
> class C(object):
> x = property(**):
>doc = "I'm the 'x' property."
>def fget(self):
> return self.__x
>def fset(self, value):
> self.__x = value
>def fdel(self):
> del self.__x
Looks good to me. You should update the pre-PEP and submit it again to
the list.
This syntax might also be a good replacement for event handling when
lambda goes away (as Guido announced in his PyCon 2005 keynote.)
button = Button(self, "Push me")
button.addHandlers(**):
def onmouseover(event):
self.textBox.setText("Are you going to push me?")
def onmouseout(event):
self.textBox.setText("Too shy. Goodbye.")
def onclick(event):
self.textBox.setText("Ouch!")
oncontextmenu = self.oncontextmenu
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: PyGTK vs. wxPython
Szabolcs Nagy wrote: > it's not quite true since the latest stable release (2.6.0.0) > > see the new wx doc (it's generated with epydoc so it's not for C++): > http://www.wxpython.org/docs/api/ > > i used wxPython with XRCed a few times and i liked the way it works > (worked under linux and win as well for me) +1. I've had a lot of success with XRCed, but no success with wxGlade. The UI in XRCed needs polish but it's easy enough to use. Also, wxGlade wants to spew generated Python code, while XRCed generates a nice resource file that wx can load easily. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Trigraph Idiom - Original?
Jeff Winkler wrote: > I've come up with a trigraph idiom, am curious if it's been done before > (probably). I like to use trigraphs occasionally. > > Scenario: I'm doing an RSS->Javascript conversion for our intranet. I'd > like to use different CSS class if a post is new. Code: > > hoursOld=abs(time.time()-time.mktime(i.modified_parsed))/3600 > cssClass=['rssLink','rssLinkNew'][hoursOld<12] > entry='%s' % > (cssClass,i['link'],i['title']) > > So, ['rssLink','rssLinkNew'] is index by boolean value- False:0, or > True:1. > > I realize many will find this hideous, but 3 lines of code to do > something simple seems equally bad. Thoughts? Is there a better way? The need for ternary expressions seems to center around presentation to humans. I find myself using such expressions frequently in presentation templates or in code that prepares information for templates. You're using it the same way. I always write it this way: condition and truecase or falsecase This works as long as truecase evaluates as a true value. (If it doesn't, the result of the expression is falsecase.) So I would write the cssClass assignment like this: cssClass = (hoursOld < 12) and 'rssLinkNew' or 'rssLink' The C equivalent is more readable, but this is close enough. It's the same trick people use in shell scripts. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Challenge ahead [NEW] for riddle lovers
pythonchallenge wrote: > For the riddles' lovers among you, you are most invited to take part > in the Python Challenge, the first python programming riddle on the net. > > You are invited to take part in it at: > http://www.pythonchallenge.com That was pretty fun. Good for a Friday. Too bad it comes to an abrupt "temporary end". Shane P.S. I hope I didn't hammer your server on step 3. I was missing the mark. :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Challenge ahead [NEW] for riddle lovers
darren kirby wrote: > quoth the Shane Hathaway: > >>pythonchallenge wrote: >> >>>For the riddles' lovers among you, you are most invited to take part >>>in the Python Challenge, the first python programming riddle on the net. >>> >>>You are invited to take part in it at: >>>http://www.pythonchallenge.com >> >>That was pretty fun. Good for a Friday. Too bad it comes to an abrupt >>"temporary end". >> >>Shane >> >>P.S. I hope I didn't hammer your server on step 3. I was missing the >>mark. :-) > > > You're not the only one. This is where I am currently stuck. It's starting to > hurt my head. > > There are 478 results in the form *BBBsBBB* but the thing said 'exactly' > right, well there are 10 results in the form *sBBBsBBBs* > None of them seem to work... I did the same thing. Now just combine those ten results. Shane -- http://mail.python.org/mailman/listinfo/python-list
Separate Address number and name
I have the following sample from a data set and I am looking to split the address number and name into separate headings as seen below. FarmID Address 1 1067 Niagara Stone 2 4260 Mountainview 3 25 Hunter 4 1091 Hutchinson 5 5172 Green Lane 6 500 Glenridge 7 471 Foss 8 758 Niagara Stone 9 3836 Main 10 1025 York FarmID AddressNumAddressName 1 1067 Niagara Stone 2 4260 Mountainview 3 25Hunter 4 1091 Hutchinson 5 5172 Green Lane 6 500 Glenridge 7 471 Foss 8 758 Niagara Stone 9 3836 Main 10 1025 York I have struggled with this for a while and know there must be a simple method to achieve this result. -- https://mail.python.org/mailman/listinfo/python-list
Re: Separate Address number and name
> I don't have any code to split that part up. There is other information > following the street name such as street suffix, city, province, postal code, > etc. I have been able to split the rest of it up based on certain criteria > but have had no luck with splitting up the street name from the street number. -- https://mail.python.org/mailman/listinfo/python-list
Re: Separate Address number and name
inHandler = open(inFile, 'r')
outHandler = open(outFile, 'w')
outHandler.write('ID\tAddress\tStreetNum&Name\tSufType\tDir\tCity\tProvince\tPostalCode\n')
for line in inHandler:
str = line.replace('FarmID\tAddress','')
outHandler.write(str[0:-1])
str = str.replace(', ON', '\t ON\t')
str = str.replace(' Rd,', '\t Rd\t \t')
str = str.replace(' Rd ', '\t Rd\t \t')
str = str.replace(' St,', '\t St\t \t')
str = str.replace(' St ', '\t St\t \t')
str = str.replace(' Ave', '\t Ave\t \t')
str = str.replace(' Pky', '\t Pky\t \t')
str = str.replace(' Lane, ', '\t Lane\t \t')
str = str.replace(', Lane, , Rd,', ' Lane\t Rd\t')
str = str.replace(' Dr', '\t Dr\t \t')
str = str.replace(' Sq', '\t Sq\t \t')
str = str.replace(' Pl', '\t Pl\t \t')
str = str.replace('\t \tN,', '\tN\t')
str = str.replace('\t \t N,', '\tN\t')
str = str.replace(' , North ', ' N\t')
str = str.replace(' ,S ', ' S\t')
str = str.replace(' , South ', ' S\t')
str = str.replace('\t \tE,', '\tE\t')
str = str.replace(' , East ', ' E\t')
str = str.replace('\t \tW,', '\tW\t')
str = str.replace('\t \t West', '\tW\t')
str = str.replace(',.', '.')
str = str.replace(',', '\t')
str = str.replace(',,', '\t')
str = str.replace(', Service', ' Service')
str = str.replace('\t \t\t', '\t\t')
outHandler.write(str[1:])
inHandler.close()
outHandler.close()
That is the code i currently have.
The following is a sample of the data. There are hundreds of lines that need to
have an automated process of splitting the strings into headings to be imported
into excel with theses headings
ID Address StreetNum StreetName SufType Dir City Province PostalCode
1 1067 Niagara Stone Rd, W, Niagara-On-The-Lake, ON L0S 1J0
2 4260 Mountainview Rd, Lincoln, ON L0R 1B2
3 25 Hunter Rd, Grimsby, E, ON L3M 4A3
4 1091 Hutchinson Rd, Haldimand, ON N0A 1K0
5 5172 Green Lane Rd, Lincoln, ON L0R 1B3
6 500 Glenridge Ave, East, St. Catharines, ON L2S 3A1
7 471 Foss Rd, Pelham, ON L0S 1C0
8 758 Niagara Stone Rd, Niagara-On-The-Lake, ON L0S 1J0
9 3836 Main St, North, Lincoln, ON L0R 1S0
10 1025 York Rd, W, Niagara-On-The-Lake, ON L0S 1P0
--
https://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] Message passing syntax for objects
So, by introducing this collaboration mechanism with a syntax that defines it as sending and receiving things that are *not* arbitrary objects, the language would naturally reinforce a more thoroughly decoupled architecture? Sent from my iPad On Mar 17, 2013, at 8:53 PM, Mark Janssen wrote: > Hello, > > I just posted an answers on quora.com about OOP (http://qr.ae/TM1Vb) > and wanted to engage the python community on the subject. > > Alan Kay's idea of message-passing in Smalltalk are interesting, and > like the questioner says, never took off. My answer was that Alan > Kay's abstraction of "Everything is an object" fails because you can't > have message-passing, an I/O task, working in the same space as your > objects -- they are two very different functionalities and they have > to be preserved **for the programmer**. > > This functional separation made me think that Python could benefit > from a syntactical, language-given separation between Classes and the > messages between them, to encourage loosely-coupled, modular OOP. > Something that OOP has always promised but never delivered. > > I think we should co-opt C++'s poorly used >> and << I/O operators > (for files) and re-purpose them for objects/classes. One could then > have within interpreter space, the ability to pass in a message to an > object. > 42 >> MyObject #sends 42 as a message into MyObject > > The Object definition would then have special methods __in__ to > receive data and a special way of outputing data that can be caught > __str__(?). > > I'm hoping the community can comment on the matter > > Thanks, > > Mark > Tacoma, Washington > ___ > Python-ideas mailing list > [email protected] > http://mail.python.org/mailman/listinfo/python-ideas -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Inline Import
Benji York wrote: > Shane Hathaway wrote: > >> Benji York wrote: >> >>> OK, good. You won't have to worry about that. :) >> >> >> You didn't give a reason for disliking it. > > > Oh, I don't particularly dislike it. I hadn't come up with a reason to > like or dislike it, other than a predilection for the status quo. I definitely sympathize. I don't want to see random changes in Python, either. The inline import idea was a bit intriguing when it surfaced, though. :-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Paul McGuire wrote:
> "Paul McGuire" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>
>
>>Well *I'm* certainly looking forward to learning some new tricks! My
>>(non-cheat) version is a comparatively-portly 245, and no alternatives are
>>popping into my head at the moment!
>>
>>-- Paul
>>
>>
>
> down to 2 lines, 229 characters
I'm down to 133 characters (counted according to 'wc -c') on a single
line. It contains about 11 whitespace characters (depending on what you
consider whitespace.) It's way too tricky for my taste, but it's fun to
play anyway. Has anyone done better so far? Here's a hint on my
strategy: the code contains three large integers. :-)
Also, here's another cheat version. (No, 7seg.com does not exist.)
import urllib2
def seven_seg(x):return urllib2.urlopen('http://7seg.com/'+x).read()
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Tim Hochberg wrote: > Paul McGuire wrote: > >>"Shane Hathaway" <[EMAIL PROTECTED]> wrote in message >>news:[EMAIL PROTECTED] >> >> >>>I'm down to 133 characters (counted according to 'wc -c') on a single >>>line. It contains about 11 whitespace characters (depending on what you >>>consider whitespace.) It's way too tricky for my taste, but it's fun to >>>play anyway. Has anyone done better so far? Here's a hint on my >>>strategy: the code contains three large integers. :-) >>> >> >>With 1 large integer, I am down to 200 characters on 1 multiline line. I >>can guess why you have 3 integers vs. 1, but I don't see how that saves you >>any characters of code. > > > This is interesting. I have a six line solution that is 133 characters > long. 133 characters according to 'wc -c'? If so, that will surely win style points over my one-line monster. But I bet someone's going to do better (without cheating.) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
I just found a 125 character solution. It's actually faster and more readable than the 133 character solution (though it's still obscure.) It depends on Python 2.4. If Python 2.3 compatibility is required for the contest, I have to add 4 characters. Shane [EMAIL PROTECTED] pycontest_01]$ wc -c seven_seg.py 125 seven_seg.py [EMAIL PROTECTED] pycontest_01]$ python test.py . -- Ran 1 test in 0.084s OK -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Andrew Durdin wrote: > On 12/28/05, Shane Hathaway <[EMAIL PROTECTED]> wrote: > >>I just found a 125 character solution. It's actually faster and more >>readable than the 133 character solution (though it's still obscure.) > > > Having spent a good deal of time and effort, and not getting below 144 > characters, I am now very eager to see how these shorter versions > work, particularly the 6 120-character solutions. I also expect that > someone (I'll certainly give it a try) will produce a yet shorter > version after the contest closes, based on the knowledge from the > winning solutions. Roberto Alsina fully documented his 120 byte solution here: http://www.pycs.net/lateral/weblog/2005/12/29.html#P333 My 120 byte solution is equivalent. Many others probably did the same thing. Python's philosophy of one way to do it seems to be leading us all down the same road. However, it still feels like there's room for improvement. I have a 123 byte solution based on a fairly repetitive 21 character string; if I could compress the representation of the string, it could win. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Tim Hochberg wrote:
> g=''.join;seven_seg=lambda i:g(
> g(' _|x|'[ord("~$]m'k{d\x7fo"[int(n)])>>s&j]
> for n in i for j in(2,1,4))+'\n'for s in(6,0,3))
>
> I've replaced the unprintable characters and added some preemptive
> linebreaks so that hopefully this won't get too munged. It's all clear
> now, right? Two hints: 6,0,3->row, 2,1,4->column and the 6 and 1 have to
> be where they are to exploit the property that the top row only ever has
> a center character in it. That way things can be encoded in 7-bits and
> fit in a character string that Python likes. The order of the other loop
> indices is mostly accidental but not all permutations may work.
I worked on a similar solution, but didn't have the idea of iterating
over a series of masks as you did with the 'j' variable. Good work.
> I'm off to try to figure out how to do it the other way now, before the
> code gets revealed.
We should have more contests like this. While the skills I applied for
the contest don't have much practical value, the mental exercise was great.
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Szabolcs Nagy wrote:
> my two solutions (well I wasn't so clever to encode everything in
> strings instead of numbers, but at least it won't give warnings about
> non ascii characters):
> 128:
> j,seven_seg=''.join,lambda s:j(j(' |_ |'[i>>3*int(c)&b]for c in s for b
> in(4,2,1))+'\n'for i in(306775170,1060861645,524130191))
>
> 122:
> seven_seg=lambda s,j=''.join:j(j(' _ _|_| |_
> |'[i>>3*int(c)&14:][:3]for c in s)+'\n'for i
> in(8208,934111592,664455910))
FWIW, here's my 121 character solution that's fully printable. (Line
breaks added.)
seven_seg=lambda s,j=''.join:j(j(
' _ _| |_ |_|'[ord('^-TR5bfmvr'[int(c)])/b%8*2:][:3]for c in s)+'\n'
for b in(64,8,1))
This solution uses the 7 lower bits of every encoded byte, rather than
the 7 upper bits that the 8 bit solutions use. 8 bits make it possible
to avoid multiplying by 2, but I wanted this version to use pure ASCII,
so I had to multiply by 2. The choice to divide rather than shift is
important because it allowed me to eliminate parentheses.
It's interesting that there seems to be no room for special cases. For
example, the top row has only two states and those states are not used
by the other rows. Therefore, the top row could be computed in a much
simpler way than the other two rows, and the other two rows could be
simplified by having only 5 possible states rather than 7. However, all
my attempts to exploit this property produced larger code.
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
André wrote: > For the few that might be interested, I will be posting the details of > a 117 character long solution to the challenge on my blog > http://aroberge.blogspot.com/. > > Enjoy! You took advantage of prime numbers, enabling you to extract encoded information using a single modulus operation rather than shift and mask. Brilliant strategy. Congratulations. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: python coding contest
Claudio Grondi wrote: > so I tried all which made sense in the context of '0' conversion and > found out, that it should be the > >' _ |_|_ _| |' > > not the at http://aroberge.blogspot.com/ > >' _ |_|_ _| |' The HTML source has the three spaces. If the code had been surrounded by a "pre" tag, it would have rendered correctly. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Spiritual Programming (OT, but Python-inspired)
[EMAIL PROTECTED] wrote: > Perhaps this is one reason why programmers are so passionate, and even > religious, about their programming tools; because they intuitively > sense that we are dealing with ideas that, however crudely, mirror > eternal realities of immense significance. While I don't associate any spiritual significance with programming, I do think that the choices we've made in the field of programming reflect deeply upon human cognition. A modern computer is a thoroughly abstract mathematical machine, so programmers can choose almost any abstraction to solve a problem. But it turns out that some abstractions fit our minds better than others, so programmers usually apply a small set of abstractions many times. So if we could categorize and chart the space of all programming abstractions that we have found most useful, we might learn a little about how our minds work. The research could be valuable for AI. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: libpython2.4.so
Levi Campbell wrote: > what is /usr/lib/libpython2.4.so used for? > On many systems, it is the core of Python, and /usr/bin/python2.4 is a small wrapper around it. Here's what I see on my box: [EMAIL PROTECTED] usr]$ ls -l bin/python2.4 lib/libpython2.4.so.1.0 -rwxr-xr-x 1 root root4072 Oct 3 12:35 bin/python2.4 -r-xr-xr-x 1 root root 1009832 Oct 3 12:35 lib/libpython2.4.so.1.0 Shane -- http://mail.python.org/mailman/listinfo/python-list
Py2exe & (Py)QT4
I'm having trouble using Py2exe with a PyQT-based python project. Or possibly I'm having a problem with PyQT and Py2exe makes it apparent. Whichever it is, I run into trouble with importing QtCore and QtGui. The error reported is: Traceback (most recent call last): File "xmlEdit.py", line 3, in ? File "PyQt4\QtCore.pyc", line 12, in ? File "PyQt4\QtCore.pyc", line 10, in __load ImportError: DLL load failed: Invalid access to memory location. Line 10 is the import of QtCore.pyd, which is included in the dist folder. Curiously, this is the same error I get when I input "from PyQt4 import QtCore, QtGui" into IDLE, so I'm not even sure it's a Py2exe issue. I'm not quite sure what the error means, only that it isn't that the file is not found (a Module Not Found error occurs if I remove QtCore.pyd). It run just fine from the command line. Can anyone shed some light on this? QT version is 4.1.3. Python is 2.4.3 and PyQT is 4.0.1. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the name of a class
John Salerno wrote: > >>> class Foo(object): > pass > > >>> dir(Foo) > ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', > '__hash__', '__init__', '__module__', '__new__', '__reduce__', > '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__'] > > Hmmm. Don't forget to file a bug. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding the name of a class
John Salerno wrote: > Shane Hathaway wrote: > >> Don't forget to file a bug. > > I'm reluctant to call it a bug just yet. Here's more stuff below. > There's obviously a difference between old- and new-style classes. It > seems that as far as new-style is concerned, __name__ is an attribute of > __class__ (along with a bunch of other stuff), but not of Foo itself. I'm not sure what you're saying. The class of a class is the 'type' builtin, unless metaclasses are involved. So your expression "dir(Foo.__class__)" is equivalent to "dir(type)", and the 'type' builtin happens to have a __name__ attribute that dir() notices. Take a look: >>> class Foo(object): ... pass ... >>> Foo.__class__ is type True >>> Foo.__name__ 'Foo' >>> Foo.__class__.__name__ 'type' The bug is that the expression "dir(someclass)", where the class is a user-defined class of either new or old style, never reveals to the user that the class object has a __name__ attribute. I tested this with Python versions 2.3 through 2.5b1. This is an education issue; since that important attribute is not in the list, newcomers are not likely to discover it, and may instead use strange incantations to get the name of a class. Do you want me to file the bug? Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: What's up with the starship?
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > >> Then perhaps you or he could explain it to us less intelligent >> people in very simple terms? > > the security advisory explains that the cause of the problem is a bug > in the source code used to implement repr() for 32-bit Unicode strings, > on all Python versions from 2.2 and onwards. > > Python 2.2 was released in 2001. So, are we to infer that Starship was running Python 2.1 or earlier at the time the server was compromised? Otherwise I missed your point, sorry. The vulnerability described by PSF-2006-001 could easily lead to server compromises. AFAIK, most Linux distributions enable UCS-4 by default, and they have done so for years. To compromise a server using the PSF-2006-001 vulnerability, an intruder just needs to find a Python CGI script running on that server that converts some bad input to unicode, then cause that script to raise an error while processing the request containing the bad input. There's a good chance the script will log an error with the repr() of the bad input, allowing the intruder to mess with the stack. If the server is running a distribution-supplied build of Python, the intruder may be able to inject arbitrary code. I don't know if this concern applies to Starship specifically, but it seems to apply to thousands of web sites running Python CGIs and Python web servers. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: What's up with the starship?
Fredrik Lundh wrote: > Shane Hathaway wrote: > > > I don't know if this concern applies to Starship specifically, but it > > seems to apply to thousands of web sites running Python CGIs and > > Python web servers. > > so are we seeing thousands of web sites running Python CGIs and web > servers being attacked right now? No, but it often takes a long time for servers to get patched, so the window for intruders is going to be open for a while. I'm trying to understand: a) how urgent and/or exploitable this is, b) how I can check whether a given Python installation (running on a server) has been patched, and c) whether the security advisory downplays the risk more than it should, since it appears that many Zope/Plone web servers are vulnerable. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Exploiting Dual Core's with Py_NewInterpreter's separated GIL ?
robert wrote: > I'd like to use multiple CPU cores for selected time consuming Python > computations (incl. numpy/scipy) in a frictionless manner. > > Interprocess communication is tedious and out of question, so I > thought about simply using a more Python interpreter instances > (Py_NewInterpreter) with extra GIL in the same process. I expect to > be able to directly push around Python Object-Trees between the 2 (or > more) interpreters by doing some careful locking. > > Any hope to come through? If possible, what are the main dangers? Is > there an example / guideline around for that task? - using ctypes or > so. > > Or is there even a ready made Python module which makes it easy to > setup and deal with extra Interpreter instances? If not, would it be > an idea to create such thing in the Python std libs to make Python > multi-processor-ready. I guess Python will always have a GIL - > otherwise it would loose lots of comfort in threaded programming I'd like to mention mod_python, which creates multiple interpreters inside Apache. It does this transparently and works well. There is no apparent lock contention between the interpreters, because no Python objects are shared. Also, I'd like to make the observation that it is not always necessary to share the entire interpreter (ala threads) in order to take advantage of multiple cores. I think Python only needs a nice way to share a relatively small set of objects using shared memory. POSH goes in that direction, but I don't think it's simple enough yet. http://modpython.org/ http://poshmodule.sourceforge.net/ Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Browser Pygame Plug-in?
Gregory Piñero wrote: > Are there security issues too? Would you remove potentially harmful > Python libraries for the plugin, not allow system calls, etc? Would > you only allow file system access in one area? Ah, so you also want to distribute untrusted Python code. That's fairly hard. There's a discussion about it on Python-Dev right now. > I guess you'd just copy however Java applets work? But run faster ;-) More or less. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Out of the box database support
Alex Biddle wrote: > Ah, so separate downloads then. At least now I know. > > Ergh... I checked the version of Python my current host is running and its > 2.2. > > ...ergh This is why you really want a VPS (virtual private server). The cost is similar to a web host but you get to choose your own technologies and versions. Several companies come to mind: http://westhost.com/ http://jvds.com/ http://linode.com/ I'm using Westhost, which provides Python 2.2 if you ask for it, but I compiled Python 2.4 instead. I didn't even have to ask whether I was allowed to do that. :-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Browser Pygame Plug-in?
Gregory Piñero wrote: > I was just idley curious on what it would take to make a web plug-in > for Pygame. I'm picturing it working the way my browser currently > shows flash games. Is such an idea even possible? Has anyone > attempted this? I once played with a similar idea. Yes, it's possible, although platform differences make the idea non-trivial. Specifically, I ran into difficulty when I discovered that it's not easy on Linux to load a dynamic library (such as libpython.so) which in turn loads other dynamic libraries (such as native Python modules), since the library search path is not modifiable at runtime. A possible workaround is to launch a separate process which projects into the browser window. I don't know what surprises may pop up on other platforms. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Out of the box database support
Alex Biddle wrote: > Ah, so separate downloads then. At least now I know. > > Ergh... I checked the version of Python my current host is running and its > 2.2. > > ...ergh This is why you really want a VPS (virtual private server). The cost is similar to a web host but you get to choose your own technologies and versions. Several companies come to mind: http://westhost.com/ http://jvds.com/ http://linode.com/ I'm using Westhost, which provides Python 2.2 if you ask for it, but I compiled Python 2.4 instead. I didn't even have to ask whether I was allowed to do that. :-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Out of the box database support
Alex Biddle wrote: > Ah, so separate downloads then. At least now I know. > > Ergh... I checked the version of Python my current host is running and its > 2.2. > > ...ergh This is why you really want a VPS (virtual private server). The cost is similar to a web host but you get to choose your own technologies and versions. Several companies come to mind: http://westhost.com/ http://jvds.com/ http://linode.com/ I'm using Westhost, which provides Python 2.2 if you ask for it, but I compiled Python 2.4 instead. I didn't even have to ask whether I was allowed to do that. :-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Browser Pygame Plug-in?
Gregory Piñero wrote: > Shane Wrote: >> Ah, so you also want to distribute untrusted Python code. That's fairly >> hard. There's a discussion about it on Python-Dev right now. > > Well, I want to write a game in Pygame, and people can just go to my > website and play it within their browser. I guess that would be > untrusted code? If they download your code with the plugin, your code is trusted. If they download it separately in a browser session, it's untrusted. So one way to get started quickly is to just mix your code with the plugin, and not attempt to enable execution of untrusted code. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python Papers Edition One
[EMAIL PROTECTED] wrote: > Ben Finney wrote: >> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: >> >>> Yes, it's true that you can't resell copies of The Python Papers for >>> personal profits, but you may derive from it, reproduce and >>> propagate it. You're quite right to point it out. >> Then please revise the false statement that the publication is "free >> as in beer and freedom", or make it true by releasing the documents >> under a license that does grant conventional free-software freedoms. >> >> -- >> \ "They can not take away our self respect if we do not give it | >> `\ to them." -- Mahatma Gandhi | >> _o__) | >> Ben Finney > > I thought I just had. In what way does the statement "Yes, it's true > that you can't resell copies of The Python Papers for personal profits, > but you may derive from it, reproduce and propagate it" not provide > such a revision and clarification? Seriously, let me know what exact > statement you feel needs to be made, and I will endorse it accordingly > if it is accurate. The phrase "free as in freedom" is commonly understood differently from the way you are using it. Free as in freedom usually grants the right to distribute for a fee. Many Linux distributors depend on that right; otherwise they wouldn't have the right to sell CDs. IMHO your licensing terms are fine; you don't need to switch from the CC license. Just avoid the term "free as in freedom", since the Free Software Foundation has assigned that phrase a very specific meaning. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Subprocess with a Python Session?
Calvin Spealman wrote:
> No matter what I do I cant get the following code to do what I expect.
> I hadn't used subprocess t o read and write to pipes of a
> still-running app, and I just can't seem to get it right. What gives?
>
> import subprocess
>
> p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
> p.stdin.write('print 10\n')
> assert p.stdout.readline() == '10\n'
Make sure the pipes are unbuffered. Launch the process with "python -u"
and flush() the streams after writing. (That's the issue I've
encountered when doing this before.)
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: first release of PyPy
Torsten Bronger wrote: > Hallöchen! > > "Kay Schluehr" <[EMAIL PROTECTED]> writes: > > >>[...] >> >>[...] Once You get enough speed out of the PyPy-runtime and the >>community shifts to it the PEP-process degenerates in the view of >>a PyPythonista to discussions about aspects of the std-objectspace >>and language design patterns. There will be some CPython >>compliance - that's all. > > > Please could somebody explain to us non-CS people why PyPy could > have speed features CPython can't have? The idea is to shift more of the responsibility to optimize code from the human to the computer. Since C code is at a low level, the computer can only infer low level intent and thus perform low level optimizations. Humans optimize C by making thousands of speed-oriented decisions directly in the code. Python code is at a much higher level, which should enable the computer to discover more of the programmer's intent and perform deep optimizations. In the end, the computer's automated optimization could turn out better than a human's manual optimization. Thus, by expressing the Python interpreter in a high level manner, PyPy is a first step toward deep optimizations that aren't possible to automate in C. Even if things don't turn out that way, note that each generation of programming languages builds on its predecessors, and PyPy could help bootstrap the next generation. Assemblers first had to be written in machine code; when it was possible to write assemblers in assembly, people started writing complex grammars and came up with C. C compilers first had to be written in assembly; when it was possible to write C compilers in C, people started inventing high level languages. Now people are experimenting with high level compilers written in high level languages. Where will this pattern lead? Who knows. :-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: first release of PyPy
Mike Meyer wrote: > Basically, there's a *lot* of history in programming languages. I'd > hate to see someone think that we went straight from assembler to C, > or that people didn't understand the value of dynamic languages very > early. Yes, although I wasn't following historical events; I was following the trends of what programmers in general have used. Theory has always been far ahead of practice... and generalizations are never correct. ;-) Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Python analog of Ruby on Rails?
George Sakkis wrote:
> "Christopher J. Bottaro" wrote:
>
>
>>Cool signature, can anyone do a Python one that I can leech? =)
>>
>>-- C
>>
>
>
> Here's the transliteration in python for your address:
>
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')])
> for p in '[EMAIL PROTECTED]'.split('@')])"
BTW, here's the proper response to that religious thread that keeps
invading this list:
python -c 'print sum([ord(c) for c in "HOLYBIBLE"])'
I'm Christian and I think it's funny. ;-) Some background:
http://scriptures.lds.org/rev/13/16-18#16
http://www.greaterthings.com/Word-Number/666HolyBible.htm
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: Running a python program during idle time only
Mike Meyer wrote: > On a completely different topic, this looks like the wrong way to solve > the problem. You want to update a search engine based on changes to the > underlying file system. The right way to do this isn't to just keep > rescanning the file system, it's to arrange things so that your scanner > gets notified of any changes made to the file system. I did something like > this for my web site search engine, but that's hooked into the SCM that's > used for propogating changes to the web site. I know someone is working > on patches to the FreeBSD kernel to make this kind of thing work. It would > seem that some of the "backup" facilities that worked by keeping a mirror > of the disk on separate media would have to have used such hooks, but maybe > not. I think you're right that filesystem change notification is what Carlos needs. If you're interested in using Linux, Carlos, "inotify" is a new kernel module that can notify your program of filesystem changes. It's not folded into the mainline kernel yet, but it's a clean patch. http://www.edoceo.com/creo/inotify/ I don't know if Windows has anything like it. I'd be interested to hear if it does. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
Michael wrote:
> sorry, I'm used to working in c++ :-p
>
> if i do
> a=2
> b=a
> b=0
> then a is still 2!?
>
> so when do = mean a reference to the same object and when does it mean make
> a copy of the object??
To understand this in C++ terms, you have to treat everything, including
simple integers, as a class instance, and every variable is a reference
(or "smart pointer".) The literals '0' and '2' produce integer class
instances rather than primitive integers. Here's a reasonable C++
translation of that code, omitting destruction issues:
class Integer
{
private:
int value;
public:
Integer(int v) { value = v; }
int asInt() { return value; }
}
void test()
{
Integer *a, *b;
a = new Integer(2);
b = a;
b = new Integer(0);
}
In that light, do you see why a is still 2?
Shane
--
http://mail.python.org/mailman/listinfo/python-list
Re: anygui,anydb, any opinions?
rzed wrote: > That's all I'm talking about here. To be able to write a Python > application that can ultimately be displayed using wxWidgets or Qt > or Tkinter or Curses. Then, without having to do more than to > change which interface package is imported, to expect to see the > result displayed. To have it just happen, and to have expectations > when using Python that it *will* just happen that way. > > So what do you think? What's wrong with the picture? Why isn't > there a greater priority to work in this direction? See "The Law of Leaky Abstractions" by Joel Spolsky. http://www.joelonsoftware.com/articles/LeakyAbstractions.html As he says, "All non-trivial abstractions, to some degree, are leaky." GUI abstractions tend to leak badly. As I write this email in Thunderbird, my initial impression is that most of the controls on the screen are pretty standard and could be done with virtually any toolkit... or are they? Looking again, the "from" field has both a place for me to type and some grayed out text that reminds me I'm posting from a different account; the "to", "cc", and "newsgroup" fields are not quite combo boxes; the toolbar has pulldown buttons. So it turns out that Thunderbird is actually tied to a particular toolkit, and for good reason: GUI development has not stagnated; toolkit developers continue to invent new, interesting widgets and variations, leading to better UIs. Application developers want to take advantage of those innovations. Even anydbm is a pretty leaky abstraction. I recently tested the behavior of 5 Python dbm implementations when they run out of disk space. Most left the database in an inconsistent state, some swallowed the exception, some segfaulted, and one deadlocked. If I'm going to rely on a dbm module, I really need to know what it's going to do when it hits an exceptional condition. Thus anydbm seemed like a bad choice. Shane -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a Python app with Mozilla
Brett Cannon was doing some work with the Firefox security model to allow Python coding from within Firefox. He may have stopped doing the work because it would not lead to a PhD. I am really looking forward to seeing someone making this a possibility. Stefan Sonnenberg-Carstens wrote: > On Do, 5.07.2007, 03:45, greg wrote: > >> [EMAIL PROTECTED] wrote: >> >> >>> wxWidgets will give you native looking apps on both Linux and Windows >>> >> Well, maybe. There's more to getting a native feel than >> just using the right widgets. I once saw a Qt-based app on >> MacOSX that had tiny little buttons that were too small >> for the text they contained -- Aqua buttons just don't >> scale down like that. :-( >> >> -- >> Greg >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> >> > Qt based buttons don't either. > If some fool uses absolute positioning and sizing, > then it is not the toolkit to blame. > I'm using wxPython for instance and use the sizers and placeholders > there, so this is possible with Qt also. > So, perhaps (which I don't know) Aqua buttons simply don't permit > absolute stuff ? > Or the Aqua programmers read the user interface guidelines carefully ? > -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: wtf
Sergiy wrote: > print 1 / 2 > print -1 / 2 > > 0 > -1 > > correct? Yes. It works like the floor() function. >>> import math >>> math.floor(1.0 / 2) 0.0 >>> math.floor(-1.0 / 2) -1.0 Shane -- http://mail.python.org/mailman/listinfo/python-list
ipython environment question
"""
I'm trying to import ipython shell (which works great!) to set some
variables and then run a function (which doesn't work).
It seems like such a simple thing. Here's an example script to show
what I'm trying to do.
Run the script and then try to change the variable project_name from the
ipython prompt and then type create()
and the new value you assigned will *not* be used. I tried to use "def
create(project_name = project_name):" Even that didn't work.
What slight of hand is necessary?
import os, sys
project_name = 'python_packages'
group_name = 'sgeiger'
repo = '/var/svn'
def create(repo=repo,group_name=group_name,project_name=project_name):
#def create():
#os.system("sudo mkdir -p " + repo )
#os.system("sudo svnadmin create " + repo)
#os.system("sudo chown -R " + group_name + " " + repo)
print "sudo mkdir -p " + repo + '/' + project_name
print "sudo svnadmin create " + repo + '/' + project_name
print "sudo chown -R " + group_name + " " + repo + '/' + project_name
if __name__ == '__main__':
sys.argv.append('-cl')
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
print "Please set these variables: project_name, group_name ...and
then type create()"
### I even tried to put all my
ipshell() # this call anywhere in your program will start IPython
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Watch Television online for free.
Watch television online now for free. Includes television channels from all over the world and sport events from all over the world including, NBA, Soccer, Motor Racing and much more. http://homepage.ntlworld.com/louise.randall41 -- http://mail.python.org/mailman/listinfo/python-list
Re: A best approach to a creating specified http post body
Why not use scotch.recorder? Dave Borne wrote: I need to build a special http post body that consists of : name=value +\r\n strings. Problem is that depending on operations the number of name,value pairs can increase and decrease. Values need to be initialized at runtime, so storing premade text files is not possible. I'm not completely understanding your problems here. Can you explain why urllib.urlencode wouldn't work? (http://docs.python.org/lib/module-urllib.html) Thanks, -Dave -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a distro of python... What would you include in it?
This is for Windows only, but since your target audience is newbies, that might be fine. Python Sumo-Distribution for Windows - Freely downloadable Python distributions for Windows with many extra packages already installed and ready for use. -- http://code.enthought.com/enthon/ BJörn Lindqvist wrote: On 30 May 2007 08:25:48 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: I am creating a distro of Python to be licensed as GPL am wondering, what would anyone suggest as to 3rd party modules being put into it (non-commercial of course!)? I know I'd put MySQLdb into it at the very least. Any suggestions? If your distro is to be GPL-licensed, does that mean that you want your components to be GPL too? If so, the number of components you can select is reduced drastically. I'd like a distro with a good IDE, GUI toolkit (PyGTK for example), Django and PyGame. Something you could point a newbie to and they would be able to create "real" applications with, without needing to download hundreds of dependencies. -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Off Topic: What is the good book to learn Python ?
Here are some excellent online books and tutorials to get started with: http://www.python.org/doc/tut/ http://www.ibiblio.org/obp/thinkCSpy/ http://www.python.org/topics/learn/prog.html http://www.python.org/topics/learn/non-prog.html http://docs.python.org/lib/ http://diveintopython.org/ http://gnosis.cx/TPiP/ http://rox.sourceforge.net/basic_python.html Here are some lists of books you can read online: http://www.techbooksforfree.com/perlpython.shtml http://en.wikibooks.org/wiki/Programming:Python http://wiki.python.org/moin/PythonBooks Some books: Byte of Python - online: http://www.byteofpython.info/files/120/byteofpython_120.pdf Quick Tour of Python - online: http://stsdas.stsci.edu/pyraf/doc/python_quick_tour/python_quick_tour.pdf Python in Nutshell - online: http://files.nixp.ru/books/programming/python/O%27Reilly%20--%20Python%20In%20A%20Nutshell.chm Python Standard Library - online: http://effbot.org/zone/librarybook-index.htm Python tutorial - online: http://www.ensta.fr/~enstar/doc/python/Python-Docs-2.4-PDF/tut.pdf kaens wrote: On 30 May 2007 11:25:22 -0700, Katie Tam <[EMAIL PROTECTED]> wrote: I am new to this filed and begin to learn this langague. Can you tell me the good books to start with ? Katie Tam Network administrator http://www.linkwaves.com/main.asp http://www.linkwaves.com -- http://mail.python.org/mailman/listinfo/python-list If you're experienced with other programming languages, I'd recommend python in a nutshell, or perhaps programming python. I personally just skimmed through the online tutorial, and kept the library and api references handy. Orielly publishers almost always have excellent books on learning new programming languages. I would also recommend to stay away from any "for dummies" or "in x (hours/days)" books. They can be decent introductory material, but unless you are really really new to programming, you probably wouldn't be getting enough information to justify the cost of the book (and a lot of times they have a lot of bad practices in them) Good luck! -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Mocking OpenOffice in python?
My take was that this is an effort to manipulate these files without the need for Open Office, so I replied as follows: Open Office files (.ods and perhaps .odt) are just zipped or gzipped. Unpack that and then you are dealing with manipulating regular text files--probably HTML. Carsten Haese wrote: On Wed, 2007-03-14 at 01:39 -0700, PaoloB wrote: Hi everyone, during our development, we need to write some unit tests that interact with OpenOffice through pyUno. Is there anyone who has got any experience on it? As OpenOffice is quite a large beast, and interaction is rather complex, I would like to know if there is someone who is willing to share experience (and, possibly, code). I have some experience with pyuno, but your question is very open-ended. It would be helpful if you asked specific questions or gave more background on what kind of interaction you're trying to achieve. The generic answer to your request for code examples is that there's a tutorial with example code at http://udk.openoffice.org/python/python-bridge.html, and then there's the API documentation at http://api.openoffice.org/DevelopersGuide/DevelopersGuide.html. -Carsten -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Raymond: It looks to me as if you are trying to turn a generator into a list in the final line. That doesn't work. Raymond Hettinger wrote: To make the solutions equi-probable, a simple approach is to recursively enumerate all possibilities and then choose one of them with random.choice(). Since people are posting their solutions now (originally only hints were provided for the homework problem), here's mine: def genpool(n, m): if n == 1: yield [m] else: for i in xrange(1, m): for rest in genpool(n-1, m-i): yield rest + [i] import random print random.choice(list(genpool(n=4, m=20))) -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: dict.items() vs dict.iteritems and similar questions
# Just by looking at the output, it seems pretty obvious that xrange
would be more memory effcient for large ranges:
print "With range():",range(100,200)
print
print "With xrange():",xrange(100,200)
d = {1:2,2:3,3:4}
d.items()
d.iteritems()
# I have been curious to use Pysizer (which requires patching Python)
to demonstrate the difference.
Drew wrote:
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print key,val
Also, when is it appropriate to use range() vs xrange(). From my
understanding, xrange() essentially gives you an iterator across a
range, so it should be used when iterating. Should you only use
range() when want to physically store the range as a list?
Thanks,
Drew
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: distributing python software in jar like fashion
Hi,
I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.
Thx in advance, A.
"""
Author: Shane Geiger <[EMAIL PROTECTED]>
Wed Mar 14 21:55:58 CDT 2007
Hi,
I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.
--
Here's an answer to your question.
To use this script put it in the same directory as your .py files.
See the glob.glob function below if you want to specify a directory if
you want to change that.
(I tried to keep this example simple.)
Use the 'pack_mode()' and 'unpack_mode()' function calls at the bottom
of the script to
control it.
Note: I have not tested this on Windows.
"""
import base64
import string, os
import zipfile
import glob
def readfile(f): return open(f, "r").read()
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)
zip_file_name = 'python_modules.zip'
def write_test_modules():
## some test modules
srgexample="""
print "This was base64 encoded."
"""
srgexample2="""
print "This, too, was base64 encoded, and it was imported from a zip."
"""
writefile('srgexample.py',srgexample)
writefile('srgexample2.py',srgexample2)
def test_mode_cleanup():
os.remove('srgexample.py')
os.remove('srgexample2.py')
os.remove(zip_file_name)
def pack_mode():
"""
PACKING MODE:
(you could automate this procedure if it is important to you)
Zip the source files into one .zip file for easier handling.
Base64 encode the zip file to avoid problems of including the file.
Include the base64 representation of the zip file in a docstring, a
step that you could automate (but I haven't).
"""
write_test_modules()
# open the zip file for writing, and write stuff to it
import zipfile
import glob, os
# open the zip file for writing, and write stuff to it
file = zipfile.ZipFile(zip_file_name, "w")
#for name in glob.glob("*.py"):
for name in [x for x in glob.glob("*.py") if x != __file__]: #
dont' try to zip up this file
file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
file.close()
## Here's how to watch what gets added
## open the file again, to see what's in it
#file = zipfile.ZipFile(zip_file_name, "r")
#for info in file.infolist():
#print info.filename, info.date_time, info.file_size,
info.compress_size
contents = open(zip_file_name, "r").read()
file_contents_in_base64 = base64.encodestring(contents)
print "Assign the following base64 encoded string to the
'python_modules_zip_base64' "
print " variable (after which you can use unpack mode)."
print file_contents_in_base64
# THEN MANUALLY ADD THAT to your main file.
# You could automatically edit the currently running file like this:
test_mode_cleanup()
def unpack_mode():
"""
UNPACKING MODE:
"""
python_modules_zip_base64="""
UEsDBBQIABq8bjYpPJvJJgAAACYNc3JnZXhhbXBsZS5weeMqKMrMK1FQCsnILFYo
TyxWSEosTjUzUUjNS85PSU3RU+JSAAIAUEsDBBQIABq8bjZ5v8x4SEwOc3Jn
ZXhhbXBsZTIucHkdyTEKgDAMBdC9p/h0Dk7iSXqBaCJmaFPagODpFd/6Uh/WArlcNgnhTrh5Yuep
2wpth4sKgZvA4i+r3Ueo4BxewXisLznh8wJQSwECFAMUCAAavG42KTybySYmDQAA
pIEAc3JnZXhhbXBsZS5weVBLAQIUAxQIABq8bjZ5v8x4SEwO
AACkgVEAAABzcmdleGFtcGxlMi5weVBLBQYAAgACAHcAAADFAAA=
"""
zip_file_contents = base64.decodestring(python_modules_zip_base64)
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)
writefile(zip_file_name,zip_file_contents)
# open the file, to see what's in it
#file = zipfile.ZipFile("test.zip", "r")
#for info in file.infolist():
#print info.filename, info.date_time, info.file_size,
info.compress_size
import sys
sys.path.insert(0, zip_file_name) # Add .zip file to front of path
import srgexample
import srgexample2
### you could even remove the file after using it
os.remove(zip_file_name)
pack_mode()
#unpack_mode()
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to get the previous line in a file?
lines = open('/tmp/foo.py',
"r").read().splitlines()
previous_line =
''
for line in
lines:
if "foo" in
line:
print "found foo in the current line. The previous line is: "
+
previous_line
previous_line =
line
Qilong Ren wrote:
Hi,all
I am new to this list. And I am glade I am here.
I have a question. I need to do some text processing. I need to read
from a file line by line. If some line is met with some condition, the
previous line needs some modification. How to get the info of the
previous line?
Thanks!
Qilong
Never miss an email again!
Yahoo! Toolbar
<http://us.rd.yahoo.com/evt=49938/*http://tools.search.yahoo.com/toolbar/features/mail/>
alerts you the instant new Mail arrives. Check it out.
<http://us.rd.yahoo.com/evt=49937/*http://tools.search.yahoo.com/toolbar/features/mail/>
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re:
Milton,
This isn't terribly elegant, but it gets the job done:
dirs = ['C:\Images', 'C:\Images\2006', 'C:\Images\2007', 'C:\Music',
'C:\Files', 'C:\Software', 'C:\Software\Python', 'C:\Software\iTunes']
exclude_list =
['Software','Images']
def foo(d):
retval = d
for item in exclude_list:
if item in d:
retval = False
return retval
dirs = map(foo, dirs)
dirs = filter(bool, dirs) # removes the Falses
print dirs
Milton Segura wrote:
Hello, I'm trying to exclude files from a list using the following code:
for item in dirs:
if item.find('Software') > -1:
dirs.remove(item)
elif item.find('Images') > -1:
dirs.remove(item)
let's supose dirs = ['C:\Images', 'C:\Images\2006', 'C:\Images\2007',
'C:\Music', 'C:\Files', 'C:\Software', 'C:\Software\Python',
'C:\Software\iTunes']
For some reason.. it just won't exclude them.
I'd like to know why and how to fix it.
Thanks in advance.
Milton
Explore the seven wonders of the world Learn more!
<http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE>
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: where function
a = [0,1,2,3,4,2,8,9] # method 1 print [i for i in xrange(len(a)) if a[i]==2] def where(a,val): return [i for i in xrange(len(a)) if a[i]==val] # method 2 print where(a,2) [EMAIL PROTECTED] wrote: On Mar 18, 10:48 pm, [EMAIL PROTECTED] wrote: vorticitywo: Is there a function in Python analogous to the "where" function in IDL? Python gives very elastic syntax, you can simply do: data = [0,1,2,3,4,2,8,9] print [pos for pos, el in enumerate(data) if el==2] Bye, bearophile Thank you both, a little more cumbersome than I expected, but it does the job! Thanks! -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehension help
Since you are dealing with that much information perhaps pytables would
be useful to you.http://pytables.sourceforge.net
[EMAIL PROTECTED] wrote:
Hi
I need to process a really huge text file (4GB) and this is what i
need to do. It takes for ever to complete this. I read some where that
"list comprehension" can fast up things. Can you point out how to do
it in this case?
thanks a lot!
f = open('file.txt','r')
for line in f:
db[line.split(' ')[0]] = line.split(' ')[-1]
db.sync()
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: check if files are the same on Windows
In the unix world, 'fc' would be like diff.
"""
Python example of checksumming files with the MD5 module.
In Python 2.5, the hashlib module would be preferable/more elegant.
"""
import md5
import string, os
r = lambda f: open(f, "r").read()
def readfile(f,strip=False): return (strip and stripper(r(f))) or r(f)
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)
def get_md5(fname):
hash = md5.new()
contents = readfile(fname)
hash.update(contents)
value = hash.digest()
return (fname, hash.hexdigest())
import glob
for f in glob.glob('*'):
print get_md5(f)
A crude way to check if two files are the same on Windows is to look
at the output of the "fc" function of cmd.exe, for example
def files_same(f1,f2):
cmnd= "fc " + f1 + " " + f2
return ("no differences" in popen(cmnd).read())
This is needlessly slow, because one can stop comparing two files
after the first difference is detected. How should one check that
files are the same in Python? The files are plain text.
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: using regexp
You don't even need regex.
def
split_seq(seq,size):
# this is sort of the inverse of
flatten
# Source:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044
return [seq[i:i+size] for i in range(0, len(seq),
size)]
line =
'123456789123456789'
print
split_seq(line,3)
Will that work for you?
[EMAIL PROTECTED] wrote:
hi
how can i use regexp to group these digits into groups of 3?
eg
line 123456789123456789
i have :
pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)
but this only gives the first 3. I also tried
"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .]
thanks.
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: using regexp
import re
line = '123456789123456789'
print re.findall('([0-9]{3})', line)
Shane Geiger wrote:
You don't even need regex.
def
split_seq(seq,size):
# this is sort of the inverse of
flatten
# Source:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044
return [seq[i:i+size] for i in range(0, len(seq),
size)]
line =
'123456789123456789'
print
split_seq(line,3)
Will that work for you?
[EMAIL PROTECTED] wrote:
hi
how can i use regexp to group these digits into groups of 3?
eg
line 123456789123456789
i have :
pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)
but this only gives the first 3. I also tried
"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .]
thanks.
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: Anything available that can read Microsoft .MDB files from Python?
Try installing it from source. Perhaps getting a newer verion is all you would need. If at that point you find there is a bug, report it. Diez B. Roggisch wrote: What MDBtools did you install? The RPM, a checkout from CVS, or the downloadable distribution? They're all different. The one that comes with ubuntu edgy. Just apt-get install mdbtools or something, that's it. I don't want to start a distro war here - but I always found the RPM-based distros lacking, to say the least. Diez -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Pycron for windows - please help
Here's something else you should consider: Look at the source code of
pycron. I just downloaded it. Much to my surprise, this is implemented
in about 115 lines of code.
In particular, look at the run() function. You should try adding a
try-except block around the system call to get a hint as to where the
problem lies:
try:
os.system('start ' + command)
except:
print "Unexpected error to catch:",
sys.exc_info()[0]
I've been trying to use it myself (on a Mac) since I saw you mention it
on the mailing list. I'm also having problems using pycron. Email me
directly if you figure out the problem. I'll do the same for you.
Gabriel Genellina wrote:
En Mon, 19 Mar 2007 16:00:04 -0300, Al <[EMAIL PROTECTED]> escribió:
I looked in the pycron.log file, and I noticed that for the entires of
my new job, I see "rc=4" and the end of each line. All other jobs have
"rc=0" at the end of the line. I assume then, that rc=4 is a reference
to an error code of some kind, but there is no information on this in
the readme.txt, at the pycron website, or here in groups.
Does anyone know how to troubleshhot this? Thanks in advance.
Contacting the author?
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list
Re: flattening/rolling up/aggregating a large sorted text file
Apparently you want to use this data to know how many blue circles, blue
squares, red circles and red squares. In other words, I doubt you want
to output redundant data columns, you just want this data in a more
usable format and that you don't actually need to do multiple passes
over it.
This is a fun problem to solve because it uses two very powerful tools:
cvs.dictreader and bitwise categorization.
Note: your initial data has three records with the same ID. I assumes
the ID is the unique key. So I changed the data slightly.
[EMAIL PROTECTED] wrote:
Hi,
Given a large ascii file (delimited or fixed width) with one ID field
and dimensions/measures fields, sorted by dimensions, I'd like to
"flatten" or "rollup" the file by creating new columns: one for each
combination of dimension level, and summing up measures over all
records for a given ID.
If the wheel has already been invented, great, please point me in the
right direction. If not, please share some pointers on how to think
about this problem in order to write efficient code.
Is a hash with dimension level combinations a good approach, with
values reset at each new ID level?
I know mysql, Oracle etc will do this , but they all have a cap on #
of columns allowed. SAS will allow unlimited columns, but I don't own
SAS.
Thanks.
ID,color,shape,msr1
--
001, blue, square, 4
001, red , circle,5
001, red, circle,6
ID, blue_circle, blue_square, red_circle, red_square
--
001,0,4,11,0
002 ...
--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
"""
Apparently you want to use this data to know how many blue circles, blue
squares, red circles and red squares. In other words, I doubt you want to
output redundant data columns, you just want this data in a more usable format
and that you don't actually need to do multiple passes over it.
This is a fun problem to solve because it uses two very powerful tools:
cvs.dictreader and bitwise categorization.
Note: your initial data has three records with the same ID. I assumes the ID
is the unique key. So I changed the data slightly.
--
Given a large ascii file (delimited or fixed width) with one ID field
and dimensions/measures fields, sorted by dimensions, I'd like to
"flatten" or "rollup" the file by creating new columns: one for each
combination of dimension level, and summing up measures over all
records for a given ID.
If the wheel has already been invented, great, please point me in the
right direction. If not, please share some pointers on how to think
about this problem in order to write efficient code.
Is a hash with dimension level combinations a good approach, with
values reset at each new ID level?
I know mysql, Oracle etc will do this , but they all have a cap on #
of columns allowed. SAS will allow unlimited columns, but I don't own
SAS.
Thanks.
ID,color,shape,msr1
--
001, blue, square, 4
001, red , circle,5
001, red, circle,6
ID, blue_circle, blue_square, red_circle, red_square
--
001,0,4,11,0
002 ...
"""
import string
## BITWISE CATEGORIZATION STUFF
def gNextBit(val=0):
while True:
y = 2**val
val += 1
yield y
nb = gNextBit()
categories = ['blue','red','square','circle']
#categories_value = ['blue','red','square','circle']
def bitwise_categorize(items):
d = {}
for item in items:
d[item] = nb.next()
return d
categories_dict = bitwise_categorize(categories)
#print categories_dict # {'blue': 1, 'circle': 8, 'square': 4, 'red': 2}
def get_properties(category_int):
p_list = []
for k,v in categories_dict.items():
if category_int & v == v:
p_list.append(k)
return p_list
def list_properties():
for i in range(len(categories)**2):
print "Properties for something with category_int
of",str(i),str(get_properties(i))
#list_properties()
### EXAMPLE DATA
header_fields = ['id','color','shape','msr1']
example_data = """
001, blue, square, 4
002, red , circle,5
003, red, circle,6
"""
# write out the example
import os
def writefile(f, data, perms=750): open(f, 'w').write(data) and os.chmod(f,
perms)
csv_file = "/Users/shanegeiger/temp.csv"
writefile(csv_file, example_data)
### READING IN THE DATA AND CATEGORIZING IT WITH BITWISE CATEGORIZATION
import csv
reade
Re: Create new processes over telnet in XP
This reminds me of something I once wanted to do: How can I install Python in a totally non-gui way on Windows (without the use of VNC)? I think I was telnetted into a computer (or something like that) and I was unable to run the usual Python installer because it uses a GUI. Laurent Pointal wrote: Jorgen Grahn wrote: On 23 Mar 2007 03:47:14 -0700, Godzilla <[EMAIL PROTECTED]> wrote: Hello, How do you create/spawn new processes in XP over telnet using python? I.e. I would like to create a new process and have it running in the background... Ssh -- or even rsh -- are better choices than telnet for these things. For some reason, they are not standard in Windows, though. ssh somewhere some command with arguments rsh somewhere some command with arguments compared to telnet somewhere and then performing expect-like things (basically simulating someone typing "some command with arguments" in the telnet session). + for an sshd running as a service under XP, look at CopSSH. + hope started process doesn't want a GUI... else, look at UltraVNC running as daemon, and port redirection using ssh. when I terminate the telnet connection, I would what the spawned processes to keep running until I shut it off... That's a function of the remote OS; what happens when its terminal goes away is not under the control of the client side. Maybe the process starting job can be done by a Python program running as Windows service and waiting for requests on a port (or Pyro object or Corba object...). No need for telnet/ssh connection, no logout problem. Just care of possible security problems :-) -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Join strings - very simple Q.
import string def test_join(l): print "Joining with commas: ", string.join(l,',') print "Joining with empty string: ", string.join(l,'') print "Joining same way, using another syntax: ", ''.join(l) print "Joining with the letter X: ", 'X'.join(l) print "Joining with <-> ", '<->'.join(l) l = ['a','b','c'] test_join(l) """ Example output: Joining with commas: a,b,c Joining with empty string:abc Joining same way, using another syntax: abc Joining with the letter X:aXbXc Joining with <-> a<->b<->c """ Dustan wrote: On Mar 24, 5:59 am, "Dustan" <[EMAIL PROTECTED]> wrote: On Mar 23, 1:30 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote: Mike Kent escreveu: ... New way: l=['a','b','c'] jl=','.join(l) I thank you all. Almost there ... I tried "".join(l,',') but no success ... :-( Paulo Perhaps you're doing it wrong, despite having an example right in front of you? Side by side comparison: jl=string.join(l,',') jl=','.join(l) The sequence is passed as an argument to the join method, and the delimiter is the string whose method is being called. To further demonstrate (because I got a weird email that seemed to think that my code didn't work): import string l = ['a','b','c'] string.join(l,',') 'a,b,c' ','.join(l) 'a,b,c' -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
