Jeff Schwab wrote:
> [EMAIL PROTECTED] wrote:
> > Jeff Schwab wrote:
> >
> >>What's the best way to generate a sequence of characters in Python? I'm
> >>looking for something like this Perl code: 'a' .. 'z' .
> >
> >
> import string
> >
> >
> print string.ascii_lowercase
> >
> > abcdefghi
[EMAIL PROTECTED] wrote:
> Hi,
>
> I am wondering how this is evaluated.
>
> a=(x for x in [1,2,3,4])
> p=[4,5]
>
> c=[x for x in p if x in list(a)]
>
> c is []
>
> but if I expand a first, like a = list(a)
>
> c is [4]
>
> So it seems that the "if" part don't get expanded ?
Well, for every elemen
http://www.python.org/doc/2.4.2/lib/built-in-funcs.html
or, if you want an answer in code now and don't want to read the docs
def my_import(name):
module = __import__(name)
globals()[name] = module #not a good idea
Or, seeing as how you won't be directly accessing them by name, anyways
Sam Pointon wrote:
> On the second point, a combination of sys.path, os.listdir and
> __import__ should do what you're after, although sifting through the
> whole of sys.path and subfolders from Python, rather than the
> interpreter itself, could be slow. (And it'll be redundant as well -
> __imp
[EMAIL PROTECTED] wrote:
> What is the cheapest/affordable pocket device that I can code python
> on? I think the closest I have seen is pocketpc from this page:
>
> http://www.murkworks.com/Research/Python/PocketPCPython/Overview
I would not recommend trying to code on a handheld device. Small s
Gustav HÃ¥llberg wrote:
> I tried finding a discussion around adding the possibility to have
> optional underscores inside numbers in Python. This is a popular option
> available in several "competing" scripting langauges, that I would love
> to see in Python.
>
> Examples:
> 1_234_567
> 0xdead_
Peter A. Schott wrote:
> Per subject - I realize I can copy/paste a line at a time into an interactive
> session when I'm trying to debug, but was wondering if there is any tool out
> there that allows me to copy sections of working Python scripts to paste into
> my
> interactive console and let
Peter A. Schott wrote:
> OK - I justed tested and may be doing something wrong, but it didn't work
> when I
> just tried it.
>
> I have something like this:
>
> X = "Value1"
> Y = "Value2"
> Z = "Value3"
>
> etc at the top of my script. When I copy/paste those three lines all at once
> into IDLE'
Terry Hancock wrote:
> I recently saw a claim that Mozilla XUL behaviors (normally
> scripted in Javascript) can (or perhaps will) be scriptable
> in Python.
>
> Also, "other languages such as Java or Python are supported
> through XPCOM", said about Mozilla (from Luxor website).
>
> Yes, I know se
ex_ottoyuhr wrote:
> To start with, I'm new at Python, so if this is something relatively
> ordinary or a symptom of thinking in C++, I apologize...
>
> Anyhow, I'm currently trying to write a means of generating
> genetic-programming functions in Python; the details would be a little
> much for a
ProvoWallis wrote:
> Thanks so much. I never would have been able to figure this out on my
> own.
>
> def dictionary_join(one, two):
>
> dict2x = dict( ((dict2[k], k) for k in dict2.iterkeys()))
> dict3 = dict(((k, dict2x[v]) for k,v in dict1.iteritems()))
> print dict3
>
> dict1 =
Steve Holden wrote:
> [EMAIL PROTECTED] wrote:
> > "Gary Herron" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> >
> >>--snip--
> >>So just use a def. It is constantly pointed out on
> >>this list that the lambda provides no extra expressive power, it is
> >>merely a shortcut
> >
I wasn't aware that python supported "if then else".
--
http://mail.python.org/mailman/listinfo/python-list
Why overload when you can use class methods?
--
http://mail.python.org/mailman/listinfo/python-list
But by using the builtin reduce, you need to specify a function, which
probably slows it down more than any speed-up from the loop in C.
--
http://mail.python.org/mailman/listinfo/python-list
To recognize variables that you have assigned, just look for
assignment. If your code is readible, and you know it well, you
shouldn't need the $ sign in front of everything.
--
http://mail.python.org/mailman/listinfo/python-list
< "return a if a.value == true"
< "database.query(q) unless database.connect == error
(etc)
if a.value == True:
return a
if not database.connect == error:
database.query(q)
Trading two words for one word doesn't necessarily make the code
better.
< unless false then print 1 # this prints
password = raw_input("Type in the password, please: ")
while password != "hello":
print "Incorrect password!"
Wouldn't this print "Incorrect password" untill the end of time if you
didn't supply the correct password?
--
http://mail.python.org/mailman/listinfo/python-list
re.findall(r'"?(.+?)"?(?:,|$)', yourtexthere)
--
http://mail.python.org/mailman/listinfo/python-list
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(".+?"|\S+)(?:,|$)', yourtexthere)
--
http://mail.python.org/mailman/listinfo/python-list
import random
flips = 100
results = [random.randint(0,1) for i in range(flips)]
heads = results.count(0)
tails = results.count(1)
print "Heads:%s" % heads
print "Tails:%s" % tails
I think this is more compact.
--
http://mail.python.org/mailman/listinfo/python-list
Thats like posting about Google here because the newsgroup is hosted on
Google.
--
http://mail.python.org/mailman/listinfo/python-list
Python for everything except things that need to be ridiculously
optimized for speed. Thats what C embedded in Python and Psyco enhanced
Python code is for.
Oh wait, thats still all Python...
--
http://mail.python.org/mailman/listinfo/python-list
Why make it an instance attribute? Couldn't you just look at the class
attribute? If its something that depends on each instance's value
assigned to the attribute, why not make it an instance attribute to
start with?
--
http://mail.python.org/mailman/listinfo/python-list
The code module, perhaps?
http://www.python.org/doc/2.4.1/lib/module-code.html
--
http://mail.python.org/mailman/listinfo/python-list
Try this.
re.findall(r'(.+? \(.+?\))(?:,|$)',yourtexthere)
--
http://mail.python.org/mailman/listinfo/python-list
Oops, the above code doesn't quite work. Use this one instead.
re.findall(r'(.+? (?:\(.+?\))?)(?:,|$)',yourtexthere)
--
http://mail.python.org/mailman/listinfo/python-list
One line solution.
dict(re.findall(r"'(.+?)' \| '(.+?)'(?:\s\||$)",yourtexthere))
--
http://mail.python.org/mailman/listinfo/python-list
re.replace.
I don't think there's any way to avoid it. Except maybe having an alias
email address or a fake one.
--
http://mail.python.org/mailman/listinfo/python-list
None of them are really indispensible. Map and filter cab be replaced
with list comprehensions. reduce is redundant except when multiplying a
series; there's a sum function for a reason. Lambda looks cleaner in
some cases, but you don't gain any functionality.
What really struck me, though, is the
Hrm, thought it had one. Guess it would help if I actually used regular
expression for replacement.
--
http://mail.python.org/mailman/listinfo/python-list
Well, I've never heard of a method like that for assigning variables.
I'd rather put it in the __init__ method.
--
http://mail.python.org/mailman/listinfo/python-list
With the exception of reduce(lambda x,y:x*y, sequence), reduce can be
replaced with sum, and Guido wants to add a product function.
--
http://mail.python.org/mailman/listinfo/python-list
Claiming that sum etc. do the same job is the whimper of
someone who doesn't want to openly disagree with Guido.
Could you give an example where sum cannot do the job(besides the
previously mentioned product situation?
Also, map is easily replaced.
map(f1, sequence) == [f1(element) for element i
sum(sequence[0] + [1/element for element in sequence[1:]])
I think that should work.
--
http://mail.python.org/mailman/listinfo/python-list
Okay, maybe that was too restrictive, reduce can *usually* be replaced
with sum. Sorry about that.
--
http://mail.python.org/mailman/listinfo/python-list
def flatten(iterable):
if not hasattr(iterable, '__iter__'):
return [iterable]
return sum([flatten(element) for element in iterable],[])
Recursion makes things so much shorter.
--
http://mail.python.org/mailman/listinfo/python-list
> Here's a couple of examples from my own code:
>
> # from a Banzhaf Power Index calculator
> # adds things that aren't numbers
> return reduce(operator.add,
> (VoteDistributionTable({0: 1, v: 1}) for v in electoral_votes))
return sum([VoteDistributionTable({0:1, v:1} for v in
electoral_votes]
List comprehensions are faster than generator comprehensions for
iterating over smaller sequences.
--
http://mail.python.org/mailman/listinfo/python-list
>>> import timeit
>>> t1 = timeit.Timer('list(i for i in xrange(10))')
>>> t1.timeit()
27.267753024476576
>>> t2 = timeit.Timer('[i for i in xrange(10)]')
>>> t2.timeit()
15.050426800054197
>>> t3 = timeit.Timer('list(i for i in xrange(100))')
>>> t3.timeit()
117.61078097914682
>>> t4 = timeit.Time
Take some time to learn one of the web frameworks. If your host doesn't
already have it, ask your host if they would consider adding it.
--
http://mail.python.org/mailman/listinfo/python-list
I see a total of 12 posts and 8 users.
--
http://mail.python.org/mailman/listinfo/python-list
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.
--
http://mail.python.org/mailman/listinfo/python-list
import re
name = "Robert"
f = file('phonebook.txt','r')
lines = [line.rstrip("\n") for line in f.readlines()]
pat = re.compile(name, re.I)
related_lines = [line for line in lines if pat.search(line)]
And then you write the lines in related_lines to a file. I don't really
write text to files much s
How is this different from a nested function?
--
http://mail.python.org/mailman/listinfo/python-list
I think you need to get a database. Anyways, if anything, it should
create no more than 5,000 files, since 5,000 facts shouldn't talk about
30,000 animals. There have been a few discussions about looking at
files in directories though, if you want to look at those.
--
http://mail.python.org/mailm
Oh, I seem to have missed the part saying 'or other word'. Are you
doing this for every single word in the file?
--
http://mail.python.org/mailman/listinfo/python-list
Well, the string that gets passed is more or less a function
definition, which is then called with exec. I don't see why you'd need
to write a string out with the function definition and then call it.
You could just write the function.
As for the nested functions, I had been presuming that it was
import math
class Vector:
def __init__(self, coordinates):
self.coordinates = coordinates
self.magnitude = sum([c**2 for c in coordinates])**0.5
self.direction = getangle(Vector([1]+[0 for i in
range(len(coordinates)-1)]))
def dotproduct(self, vector):
sum([a
> You missed Steven's point which is to quote the message to which you are
> replying. Not everyone is reading this list in a conveniently threaded
> form, so you need to provide some context for them to be able to follow
> along.
Ah, sorry, I didn't quite get what he was referring to.
--
http:/
Why doesn't group have an argument? group() or group(0) returns what
the pattern matched, not what it returns.
--
http://mail.python.org/mailman/listinfo/python-list
teoryn wrote:
> I've been spending today learning python and as an exercise I've ported
> a program I wrote in java that unscrambles a word. Before describing
> the problem, here's the code:
>
> *--beginning of file--*
> #!/usr/bin/python
> # Filename: unscram.py
>
> def sort_string(word):
>
Robert Kern wrote:
> That's definitely not the kind of dictionary that he wants.
>
> --
> Robert Kern
> [EMAIL PROTECTED]
>
> "In the fields of hell where the grass grows high
> Are the graves of dreams allowed to die."
>-- Richard Harter
Oh, I missed the part where he put values in a list
Robert Maas, see http://tinyurl.com/uh3t wrote:
> > From: Robert Kern <[EMAIL PROTECTED]>
> > As you can see in the datetime documentation, the module was introduced
> > in Python 2.3. I recommend updating your Python installation.
>
> What do you mean "your"?? I don't have any Python installatio
flyaflya wrote:
> I want to join some surfaces to a new big surface with alpha cannel, I want
> the new surface has same pixels(inclue r,g,b and alpha value) as the pixels
> on the source surfaces.
> my code as follow:
>
> surf = pygame.Surface((200,200))
> surf.blit(surf1, (0,0))
> surf.blit(su
Sounds somewhat like homework. So I won't just give you a code
solution. Use the regular expression(re) module to match the urls.
--
http://mail.python.org/mailman/listinfo/python-list
Ed Leafe wrote:
> On Sunday 31 July 2005 22:39, Paul Rubin wrote:
>
> > > import dabo
> > > app = dabo.dApp()
> > > dApp.start()
> > >
> > > Sorry, I couldn't do it in 5. ;-) Oh, and that includes a full menu,
> > > too.
> >
> > I get an ImportError exception when I try that. Any suggestions? N
Eric wrote:
> I am reading a book on Python and ran across and exercise that I just
> can't seem to figure out. Its pretty simple, but I just can't get
> past a certain point.
>
> The task is to create a program that flips a coin 100 times and keeps
> track of the total of heads and tails which is
a report out that the "scripting languages" in general are
> losing mindshare big time in Europe, Africa and the east in general.
> In fact, everywhere except in North America. I'm generally somewhat
> skeptical of these reports unless I can see the methodology, but it
googleboy wrote:
> for key in form.keys():Yes, I know which fields are hidden because I
> made them that way, but I am trying to figure out a way I can iterate
> over the fields and do one thing with hidden fields (assign them to
> variables that tell the form how to process) and a different th
Harlin Seritt wrote:
> I have been looking at the Python re module and have been trying to
> make sense of a simple function that I'd like to do. However, no amount
> of reading or googling has helped me with this. Forgive my
> stone-headedness. I have done this with .NET and Java in the past but
>
Ksenia Marasanova wrote:
> Hi,
>
> I have a list that contains nodes from a tree. Each node is a class
> instance, but I'll use dictionary here to simplify the example.
> So the list looks like this:
> [
> {'id': 1,
> 'name': 'Parent node',
> 'ord_number': 1,
> 'parent_id': 0,
> 'url': '/parentnod
John Machin wrote:
> Aahz wrote:
> > In article <[EMAIL PROTECTED]>,
> > John Machin <[EMAIL PROTECTED]> wrote:
> >
> >>Search for r'^something' can never be better/faster than match for
> >>r'something', and with a dopey implementation of search [which Python's
> >>re is NOT] it could be much wor
John Machin wrote:
> Devan L wrote:
> > John Machin wrote:
> >
> >>Aahz wrote:
> >>
> >>>In article <[EMAIL PROTECTED]>,
> >>>John Machin <[EMAIL PROTECTED]> wrote:
> >>>
> >>>
> >>>
Fausto Arinos Barbuto wrote:
> Ray wrote:
>
> > 1. Where are the access specifiers? (public, protected, private)
>
> AFAIK, there is not such a thing in Python.
>
> ---Fausto
Well, technically you can use _attribute to mangle it, but technically
speaking, there are no public, protected, or pr
def descend(iterable):
if hasattr(iterable, '__iter__'):
for element in iterable:
descend(element)
else:
do_something(iterable)
This will just do_something(object) to anything that is not an
iterable. Only use it if all of your nested structures are of the same
Talin wrote:
> I want to make a dictionary that acts like a class, in other words,
> supports inheritance: If you attempt to find a key that isn't present,
> it searches a "base" dictionary, which in turn searches its base, and so on.
>
> Now, I realize its fairly trivial to code something like thi
Thomas Bartkus wrote:
> On Sun, 28 Aug 2005 23:11:09 +0200, billiejoex wrote:
>
> > Hi all. I'd need to aproximate a given float number into the next (int)
> > bigger one. Because of my bad english I try to explain it with some example:
> >
> > 5.7 --> 6
> > 52.987 --> 53
> > 3.34 --> 4
> > 2.1 -->
Grant Edwards wrote:
> On 2005-08-30, Devan L <[EMAIL PROTECTED]> wrote:
> >
> > RoundToInt(2.0) will give you 3.
>
> That's what the OP said he wanted. The next bigger integer
> after 2.0 is 3.
>
> --
> Grant Edwards
Kevin Little wrote:
> I want to dynamically add or replace bound methods in a class. I want
> the modifications to be immediately effective across all instances,
> whether created before or after the class was modified. I need this
> to work for both old ('classic') and new style classes, at both
talin at acm dot org wrote:
> Thanks for all the respones :) I realized up front that this suggestion
> is unlikely to gain approval, for reasons eloquently stated above.
> However, there are still some interesting issues raised that I would
> like to discuss.
>
> Let me first respond to a few of t
Xah Lee wrote:
> what's the decision? any reference to the discussion?
>
> i thought it is better for Python to have one single recognizable logo.
> Perhaps python doesn't have a logo and the official python people
> decided it shouldn't or just doesn't have one currently?
>
> of course, a logo he
billiejoex wrote:
> Hi all. I'm sorry for a noob question like this but I'll try to ask it
> anyway.
> One of the greatest problem that may discourage a new user to choose Python
> language is it's interpreted nature.
What? The instant gratification of immediate results is not
discouraging.
> Ano
LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
> Hi all,
>
> I have a question with some code I'm writting:
>
>
> def main():
>
> if option == 1:
>
> function_a()
>
> elif option == 2:
>
> function_b()
>
> else:
>
> raise 'option has to be either 1 or 2'
>
> if itera
[EMAIL PROTECTED] wrote:
> Hey guys, i just started learning python (i usually use java/C).
>
> this has got me stumped as its not mentioned in the documentation
> (unless im skimming it every time).
>
> How does one instanciate a class from another file
import somefile
foo = somefile.class(__ini
Ray wrote:
> Hello,
>
> I'm just reading the latest edition of Python Cookbook, where it says
> in Recipe 4.2:
>
> "when the op you wanna perform on each item is to call a function on
> the item and use the function's result, use L1 = map(f, L), rather than
> L1 = (f(x) for x in L)"
>
> What is wro
Antoon Pardon wrote:
> On 2005-12-10, Duncan Booth <[EMAIL PROTECTED]> wrote:
[snip]
> >> I also think that other functions could benefit. For instance suppose
> >> you want to iterate over every second element in a list. Sure you
> >> can use an extended slice or use some kind of while. But why n
amfr wrote:
> I am writing a webserver, and I want it to be able to run python
> scripts. But when I set sys.stdin to self.rfile (using the
> BaseHTTPServer class, self.rfile is a input stream containing the
> request), the cgi module does not parse the data.
> Example script:
> import cgi
> form
Mike Meyer wrote:
> Ok, I've given it the interface I want, and made it less of an
> attractive nuisance.
>
> http://www.mired.org/home/mwm/try_python/ is now ready for people to
> play with. There's no tutorial information on it yet, that's the next
> thing to do. However, I won't be able to work
I've spent a while putting together a partially working Try Python
which handles class and function definitions. It also (used to) work
with imports, but my hacked version of jelly doesn't work with it
anymore, so only import this works as far as I know. It won't play nice
if you store the id of an
Steve Holden wrote:
> Devan L wrote:
[what I said]
> At first I thought 'the cgitb TypeError message from "import os" is
> impressively drastic :-)'. Then I realised in a later session that
> "import os" only gave an error message after I'd run &qu
Mike Meyer wrote:
> After spending time I should have been sleeping working on it, the try
> python site is much more functional. It now allows statements,
> including multi-line statements and expressions. You can't create code
> objects yet, so it's still more a programmable calculator than
> an
Mike Meyer wrote:
> "Devan L" <[EMAIL PROTECTED]> writes:
> >> If you want to try an online P{ython tool that lets you save code, try
> >> Devan L's at http://www.datamech.com/devan/trypython/trypython.py.
> > My code uses one of the recipes from th
[EMAIL PROTECTED] wrote:
> I like the form, no matter what its limitations may be. Three notes:
>
> It might be a good way to catch newbi mistakes (those are the kind I
> make :P, thereby providing a feedback loop to improved error messages.
>
> I had no trouble with from math import * followed b
Mike Meyer wrote:
> [EMAIL PROTECTED] writes:
[comments about Mike Meyer's try python, I think]
> > I had no trouble with from math import * followed by print pi, but
> > there was no >>> prompt after the result appeared .. is that part of
> > the 'closures' thing mentioned earlier?
>
> Hmm. Are y
Mike Meyer wrote:
> Xavier Morel <[EMAIL PROTECTED]> writes:
[Old message and Xavier's question]
[Mike's reply to Xavier]
>
> > Since Python doesn't have any way to secure the interface built-in,
> > i'd be interrested in that.
>
> Devan apparently doesn't have as cooperative an ISP, and is working
It's been a while since I've done anything, and I have finals, so if
anyone wants to look at some of the source, here's the somewhat cleaned
up source for bastille and modjelly. Bastille is just a
sort-of-more-secure equivalent of what the code module is, in case you
have no clue what it does since
Graham wrote:
> I've been messing around with trying to get a small sandbox like
> environment where i could execute python code in a "safe" way.
> Basically what the old restricted execution module attempted to do.
> I've written a small amount of code to get custom interpreter running,
> but i'm
Is there any safe way to create an instance of an untrusted class
without consulting the class in any way? With old-style classes, I can
recreate an instance from another one without worrying about malicious
code (ignoring, for now, malicious code involving attribute access) as
shown below.
>>> im
Ben Finney wrote:
> "Devan L" <[EMAIL PROTECTED]> writes:
>
> > Is there any safe way to create an instance of an untrusted class
>
> Why are you instantiating classes you don't trust?
>
> > without consulting the class in any way?
> If you
Michael Spencer wrote:
> Devan L wrote:
> > Is there any safe way to create an instance of an untrusted class
> > without consulting the class in any way? With old-style classes, I can
> > recreate an instance from another one without worrying about malicious
> &
91 matches
Mail list logo