> hash(b2) == hash("foo")
True
>>> d2 = {b2: "bar", "foo": "baz"}
>>> print d2
{<__main__.BrokenHash object at 0x657e10>: 'bar', 'foo': 'baz'}
(If they both fell into the same bucket, d2['foo
gt;
> Can I sign up for the next class? When will it start?
>
> Thanks much,
> Scott
> ___
> Tutor maillist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
"How many times should it be printed?")
>> print snumber
>>
>> A good robust alternative is to try int() and capture / report the
>> failure:
>>
>> text = str(input("Type in some text: "))
>> snumber = input("How many times should it be printe
is no
need to check for the existence of the other elements, so you can cut out a
lot of the checks.
Whilst your requirements are not exactly clear to me, here is how I would do
what it sounds like you need (using the same dict layout as you used
previously):
def add_record(field1, field2, field3, fi
iter__(self):
if self.holdsEntry:
yield self.entry
for child in self.children:
print "<"
for val in child: #implicit call to child.__iter__()
yield val
print ">"
Then, when the child.__iter__() is
improvement if you load the function
definition into idle, and then start typing the function name. A tooltip
will pop up, listing all the arguments it can take.
You can still use the function in exactly the same way as with **kwargs, it
just won't hand you a dictionary holding the arguments pas
ion options:
> http://mail.python.org/mailman/listinfo/tutor
>
itertools.product() also has the repeat keyword argument:
for x in itertools.product('abc', 'abc', 'abc'):
is the same as
for x in itertools.product('abc', repeat=3):
--
Rich "R
maillist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
a.pattern:
>>> import re
>>> a = re.compile("foo")
>>> a.pattern
'foo'
--
Rich "Roadie Rich" Love
ly calls the function when you
ask it for the next value. You can get similar behaviour in python2
using the imap function in the itertools module. It's extremely
useful if you need to deal with massive lists of millions of elements
- where making the list ever
Forgot to reply-all...
-- Forwarded message --
From: Rich Lovely
Date: 2009/7/1
Subject: Re: [Tutor] calculating a sort key and operator.attrgetter()
To: Vincent Davis
2009/7/1 Vincent Davis :
> I have a class with an attribute which is a list "rank_list" this i
r use
eval(raw_input(...)) for the same reasons you should never use
input(...), for in effect, that is exactly what you are using.
In fact, the python 3.0 docs recommend that form (using the new name
for raw_input), in expert scripts allowing interactive code entry.
--
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
le of writing their own script to do so, and would probably just
be annoyed at any warnings you chose to stick in. I've never gotten
to the point where I've needed input(). I'm curious to know whether
anyone on the list has.
--
Richard "Roadie Rich" Lovely, part of the J
nge(len(...)), as it adds
in the overhead of two function calls, and ruins the readability of
code. The latter is probably the most important of the two.
An even more pythonic way to do this would be a list comprehension,
http://www.python.org/doc/2.6/tutorial/datastructures.htm
27;re running - there is a maximum of only 17 passes (once
for each value of coin and note)
--
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
opython.org/) has been recommended
for people with some programming experience, and Think Python for
those without, although I'm sure that if you ask 10 members of this
list, you'll get 20 different suggestions. Real veterans get pointed
at the standard libraries, which are extremly
eeing as you appear to be treating your options as
(effectively) a char[]:
options = "".join(chr(x) for x in (opt4, opt3, opt2, opt1))
Alternatively, like Kent said, take a look at the struct module, which
will do all this for you, but is not so educational.
--
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
s a key:
dct = dict((i[0], i[1:]) for i in lst)
Then you can access it using the normal dictionary interface.
dct["3991404"][3]
This will only return the last of any repeated values (previous ones
will get overwritten during construction), so it really depends on the
behaviour you w
;ve got a
version prior to that, you'll need to rework it a little, or upgrade.
But I think this gives the general idea. I don't think there's any
more concise way of doing it than that in python.
You also might want to use print instead of writing straight to a
file, and use the te
It won't hurt code on more recent versions.
from __future__ import with_statement
import sys
buff = []
with open("path_to_input_file", "r") as fin:
for line in fin:
buff.append(" ".join(lookup(word) for word in line.split()))
with open("path_to_out
rcise for you, as I don't know the
format of the reference you're using.
--
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
know, but it makes the point.
Unless, of course, this sort of gibberish is what you're after.
Natural language parsers are one of the hardest things to create.
Just look up the word "set" in a dictionary to see why. Even if you
did work out that the second "set" was a nou
On 11 Jul 2009, at 03:15, Angus Rodgers wrote:
Wesley Chun, /Core Python Programming/, Ex. 6-13:
"[...] An atoc() was never implemented in the string module, so
that is your task here. atoc() takes a single string as input,
a string representation of a complex number [...] and returns
the eq
"C", "CA", "CB")
for position in positionsNeeded:
print position, "ALA C = %s CA = %s CB = %s" %
tuple(atoms[position].get(a,"") for a in atomsNeeded)
You would then call it with something like
$python myscipt.py > output.txt
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_
> Tutor maillist - tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
Sorry if I've sent this twice...
Why would you want to do that?
The closest you can get to that is using exec, but exec is usually
considered a code
u...@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
> Sorry if I've sent this twice...
>
> Why would you want to do that?
>
> The closest you can get to that is using exec, but exec is usually
> considered a code smell. I'd say you&
; into "values[5]", and you wonder why it's appearing in your output?
>>
>> Change the final for loop to the following, and it will do what I
>> think you want it to do.
>>
>> for k, v in atoms.iteritems():
>> print k, "ALA C = %s CA = %s
= %s" % tuple(v.get(a,"") for a in
atomsNeeded)
Check you've got the indentation (the spaces at the start of lines)
correct, exactly how it is above: this is VERY important in python.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
> Tutor maillist - tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
There is an error before there:
In the line
entry = name + '|' + email + '|' + address + '|' + telephone + '|' +
IpAddre
ng completely different.
>>> print "_".join(listOfStrings)
And_now_for_something_completely_different.
If you need to perform other operations first, you can pass a
generator expression as the argument, for example:
>>> " ".join((s.upper() if n%2 else s.lower(
e same
length as the longer file) you can use itertools.izip_longest(). Look
it up in the docs for usage.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
first inserted
entry gets an ID of 1, the next 2, and so on. This is stored in a
column in the table. The get_director_id(...) function will do
something like the following query:
cursor.execute("SELECT id FROM directors WHERE name == \"%s\"", (name,))
I don't know how
occupied position"
Finally, you might want to put the code doing the actual work into a
"if __name__ == "__main__": block, so that you can import the script
as a module, and not get extra output. Take a look at some of the
files in the standard library to see what I mean.
--
Rich &q
now the details.
>
> Thanks,
> Raj
> ___
> Tutor maillist - tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
This is usually a firewall issue. What firewall software are you
using, and do you have access to modify
#option 1
return "LT"
elif isinstance(param, str):# option 2
if os.path.isfile(param):
return "FILE"
else:
return "STR"
else:
return None
Also, file-type objects aren't instances of str...
--
Rich
Forgot to reply-all...
-- Forwarded message --
From: Rich Lovely
Date: 2009/9/9
Subject: Re: [Tutor] Python and google sites
To: andy
2009/9/9 andy :
> Hi people
>
> After searching pypy and google, I was wondering if anyone has any
> experience/knowledge of a p
tions:
> http://mail.python.org/mailman/listinfo/tutor
>
If we want to be really modern:
#Only works on python 2.5 or later. On 2.6 (or higher...), the
following line is not needed.
from __future__ import with_statement
previous_line = ""
with open('somefile.txt','
://docs.python.org/library/modules.html
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist - Tutor@python.org
To unsubscribe or change sub
ble to turn it into a one line comprehension:
myNewList = [[decorated[1] for decorated in sorted(zip(mylist[0], L),
key=lambda t: t[0])] for L in mylist]
I'm not suggesting you do it like this, but it's always useful to
learn new patterns.
--
Rich "Roadie Rich" Lovely
There
licated
the problem described. (I can't even get the eof error by hitting
enter on a newline) - I am on a mac, running from terminal.
I can only replicate the error one way:
$ cat test.py | python3
Traceback (most recent call last):
File "", line 1, in
EOFError: EOF w
st):
File "", line 1, in
EOFError: EOF when reading a line
(I also tested the script in the OP, and got identical output to that
mentioned).
This supports Kent's suggestion in a way, although maybe for a
slightly different reason.
--
Rich "Roadie Rich" Lovely
so do the same with a lambda:
print sorted(words, key=lambda o: locale.strxfrm(o[0]))
Hope that helps
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
> Tutor maillist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
FYI, open office .odc files are zip archives of xml files. It should
be trivial to access the information from them, assuming
the indexes of the (first) minimum and
maximum in the array"""
minVal = 0, a[0]
maxVal = 0, a[0]
for v in enumerate(a):
if v[1] < minVal[1]:
minVal = v
elif v[1] > maxVal[1]:
maxVal = v
return minVal[0], maxVal[0]
--
Rich
) is negative.
The first value is start, the second stop, and the third step. What
you are saying with numbers[10,0,-2], is start at 10 (facing towards
the end of the sequence), keep going until you get to 0, taking two
steps backwards each time.
I hope that clears things up a little..
--
Rich
2009/9/17 Kent Johnson :
> On Thu, Sep 17, 2009 at 11:01 AM, steve wrote:
>> On 09/17/2009 06:04 PM, Kent Johnson wrote:
>>>
>>> On Thu, Sep 17, 2009 at 8:06 AM, Rich Lovely
>>> wrote:
>>>>
>>>> 2009/9/17 Rayon:
>>>>&
illist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Is there any specific reason you need to use **kwargs?
If you omit it from function definitions, you can use normal argument format:
>>> class Demo(obje
..
>>> Demo(name='Rich', surname='Lovely', age=24)
Rich Lovely 24
<__main__.Demo object at 0x00F75230>
>>> Demo(notAName='foo')
Traceback (most recent call last):
File "", line 1, in
TypeError: __init__() got an unexpected
this? And there is no error, btw.
>
> ___
> Tutor maillist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
You are testing a string, returned by raw_input()
hing like
try:
home = os.environ['HOME']
except KeyError:
home = None
else:
print "My home directory is", home
Admittedly, I don't know what the value of $home would be after
executing the snippet above, but I'm assuming nil or null or whatever
the perl
ers.html for info. If it's not
there, the code demos another feature of isinstance most people don't
notice: the type argument can be a sequence of types, so I set Real
to a tuple of all the builtin real number types (that I can remember
off the top of my head) if the import fails.
If yo
ns are better than twice the speed
(in this simple example) of a regular for loop. With an existing
list, adding a comprehension is surprisingly fast (I was expecting
extend to be faster). I wasn't expecting the slow comprehension to be
as slow as it is, but as you can see, it is slower than a
a much larger archive of questions asked
previously - most of the posts to this list, archived at
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor
(among a few other places), are about 2.x code.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those
h xrange() and len(), I'd always
recommend using enumerate:
e.g. Your example of
for i in xrange(0, len(x)-1):
print x[i], x[i+1]
becomes
for i, v in enumerate(x[:-1]): #omitting last value in list to avoid IndexError
print v, x[i+1]
I've got to say that of the two, I prefer
rg/mailman/listinfo/tutor
>
>
Look up the itertools module:
http://docs.python.org/library/itertools.html
Specifically, cycle and izip. itertools.cycle is the trick here, izip
is a more efficient (in this case) version of zip.
--
Rich "Roadie Rich" Lovely
There are 10 types of p
d a conversion to int, and I'm catching KeyError (for
dicts), IndexError (for lists), and TypeError, for when int(choice)
fails.
This is a principle called "It's Easier to Ask Forgiveness than
Permission" (EAFP), which is one of the pythonic principles.
Hope that hel
e(mask)))
But that's maybe going a little over the top, unless we're entering an
obfuscated code contest.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
_
2009/10/15 Wayne Werner :
>
>
> On Thu, Oct 15, 2009 at 10:50 AM, Rich Lovely
> wrote:
>>
>> 2009/10/15 Wayne Werner :
>> > Hi,
>> > I'm writing a text based menu and want to validate the user input. I'm
>> > giving the option
d()
else:
queue.insert(0, PrecedentTask())
Yes, the precedentTask code could be pasted into the else block, but
what if more than one class used that precedent, or the precedent had
precedents itself, which in turn were used by multiple classes?
OK, That was pretty contrieved, and
ingle pass
d[entry.year()][entry.month()].append(entry.data())
return d
And finally, a little utility function, which DOES use recursion:
def mapToDict(d):
""""recursively convert defaultdicts into regular dicts"""
d = dict(d)
d.update((k,
distribution, which is the usually recommended python distribution for
windows.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillis
thods to click on links and so on, exactly as you would in a
browser. Basically, if you can do it with a keyboard and your
prefered webbrowser, you can do it with mechanize, and a few things
beside. (I've bypassed a (copypasta) javascript md5 function on a
forum login before now...)
--
Rich &quo
The same will happen if you try
looking at the pydocs for, for example, str or repr: they are
wrappers for magic methods on the object it is called with.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who a
; l1 = range(4)
>>> l2 = range(2)
>>> def p(x, y):
... print x, y
...
>>> map(p, l1, l2)
0 0
1 1
2 None
3 None
[None, None, None, None]
>>> def p(x, y):
... return (x or 0) + (y or 0)
...
>>> map(p, l1, l2)
[0, 2, 2, 3]
--
Rich "Roadie Rich" L
ription options:
> http://mail.python.org/mailman/listinfo/tutor
>
This, of course is a rather dirty, implementation (and probably
version) specific hack, but I can /calculate/ the sequence, using just
one line:
>>> print " ".join(str(i) for i in [x if x<2 else
>
code on the server, unless it depends on third party modules.
Uplaod it to a pastebin, send us the link, and we might consider taking a look.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
ttr__ and so on,
but that's a little more complicated.
I will however say, that the original behaviour is entirely
intentional. Python was designed to be a language used by consenting
adults who should know better than doing things that are likely to
break stuff. For instance, there is nothing t
x27;m
> hoping some Tutor could post the link.
>
> Thanks,
>
> Dick Moores
> ___
> Tutor maillist - tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
ach "quad" (the term for each number in
an IP address), multiply it by a certain constant depending on where
in the address it falls, and then adding it to the numeric address.
Perhaps there's a library function to do this, but it's a useful
learni
rtunately, this is just a simple http request, much
like you are already doing - it's just handled under the surface by
javascript.
--
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
_
es to number theory, I always turn first of all to wikipedia.
http://en.wikipedia.org/wiki/Repeating_decimal
If that doesn't help, I usually turn to Wolfram:
http://mathworld.wolfram.com/RepeatingDecimal.html
Between the two, and the links therefrom, there's probably all the
information you ne
nsiderably easier on windows,
there is the win32print package that does (most of) the heavy lifting
for you. To be honest, I'm not even sure if option four will work
_at_all_.
--
Rich "Roadie Rich" Lovely
Just because you CAN do s
The try: except: clauses allow for someone typing something like
'spam' when the program expects a number. It stops the program dying
with an error message.
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
---
(Sent from my iPod - please allow f
one arguement.
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
---
(Sent from my iPod - please allow for any typos: it's a very small
keyboard)
On 31 Oct 2008, at 20:16, [EMAIL PROTECTED] wrote:
This problem involves a callback method while using &
it's
website.
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it's a very small
keyboard)
On 9 Nov 2008, at 03:22 AM, Bap <[EMAIL PROTECTED]> wrote:
Can I use n
@list: pickle?
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it's a very small
keyboard)
On 14 Nov 2008, at 04:42 PM, "Jojo Mwebaze" <[EMAIL PROTECTED]>
wrote:
Sorry Alan,
What u de
ng a number iff not any(l[:sqrtX]). No
division involved...) If that make no sense to anyone, it makes less
sense to me, sounds ok, though...
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it's a very
Wsgi is also more portable than mod_python: mod_python is solely
available on the Apache httpd, whereas wsgi is available on many more.
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it's a very s
How about this:
def recursiveDictFactory():
return defaultdict(recursiveDictFactory)
dictOfDictsOfDictsEtc = defaultdict(recursiveDictFactory)
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it'
When you say linear, I'm assuming fitting y=mx+c, and passing through
points?
The line through points (x1, y1) and (x2,y2) is
y - y1 = (y2-y1) / (x2-x1) * (x-x1)
That multiplies out to:
y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1
That gives m = (y2-y1)/(x2-x1) and c = y1 - (y2-y1)/(x2-x1
Please use the reply-all button when responding, so that your message
gets sent to the list as well.
If that's the sort of equation you're after, than the easiest way
would probably to decide on a generalised form:
y=(2x-1)*4x
y = 4*x**2 - 4x
y=2+5(x-1)
y = 5*x - 3
y=(2x+5)+(5x-25)
y =
Yeah, you could do that, but it was quite a revelation when I
discovered itertools, and I'm just trying to share the love.
---
Richard "Roadie Rich" Lovely
Part of the JNP|UK Famille
www.theJNP.com
(Sent from my iPod - please allow me a few typos: it's a very small
k
Hi all, I was part of this list a couple of years ago, and a recent discussion
at a python dojo brought to mind something I'd seen then: a one-liner
(potentially single statement) webserver. I'm pretty sure it was posted to
this list, but I can't find it in the archives, and a google search is
-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Rich Lovely wrote:
>> Hi all, I was part of this list a couple of years ago, and a recent
>> discussion at a python dojo brought to mind something I'd seen then: a
>> one-liner (potentially single statement) w
On 10 Nov 2011, at 15:20, learner404 wrote:
>
>
> On Thu, Nov 10, 2011 at 2:14 PM, Steven D'Aprano wrote:
> learner404 wrote:
> Hello list!
>
> - myapp.py is in a "myfolder" folder that the "users" will be able to
> download and put anywhere on their Mac.
> [...]
>
> In both cases OSX compla
If you're on linux or OSX, there's /usr/share/dict/words, which has a few
thousand words. Although no plurals, which caught me out once. If you're on
windows, it's not a hard file to find.
On 10 Nov 2011, at 16:14, Alex Hall wrote:
> What about just grabbing a bit text file, such as from Proj
Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Although there's nothing wrong with taking the stack trace and
cleaning it up - perhaps dumping it to a logfile, using the Traceback
module. Asking your users to send a fi
On 4 Dec 2011, at 16:36, Modulok wrote:
>>> 2. If one wants to make a commercial software using python, how can he
>>> hide the code?
>
> While it's a valid question, it's fun to imagine it in the physical world: "We
> need to permanently weld the engine compartment closed so that no one can
>
Forgot to reply all...
-- Forwarded message --
From: Rich Lovely
Date: 13 December 2011 23:17
Subject: Re: [Tutor] A shorter way to initialize a list?
To: Kaixi Luo
On 13 December 2011 20:39, Kaixi Luo wrote:
> Hello,
>
> I want to create a list of lists of lis
itertools.product, although I can't see exactly what you want as a
final result.
The process will be something like:
turn a into list of lists of key:value pairs
call itertools.product() using star notation
(http://docs.python.org/tutorial/controlfl
8e-01
> 2.280239e-01
>
>
> Thanks with best regards,
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
You can't append to an int, you
90 matches
Mail list logo