stlib name clash when using python as ASP language
Hi guys, I have couple of simple python based active server pages that make use of httplib2 which uses gzip.py. IIS, however, also has a gzip.dll located at the iis/inetsrv path. When using ASP the iis/inetsrv path is placed as the first item in sys.path. Consequently importing httplib2 will cause the following error: import httplib2 File "c:\python24\lib\site-packages\httplib2-0.2.0-py2.4.egg \httplib2\__init__.py", line 25, in ? import gzip ImportError: dynamic module does not define init function (initgzip) This is, of course, because it tries to load the gzip.dll file instead of the gzip.py file. The following per page hack *fixes* the problem most of the time: import sys sys.path[0] = "c:/python24/lib" Strangely, even with this code loaded in every page the import error sporadically occurs. What would be the preferred way to solve this name clash? -- http://mail.python.org/mailman/listinfo/python-list
Re: stlib name clash when using python as ASP language
> You *assume* that [0] is the IIS path, but perhaps some other imported > module changed sys.path too, and now it's not the first one anymore. > If you know exactly the path, try sys.path.remove(iis_path). > > -- > Gabriel Genellina It's was a hack and definitely not meant to go in to production ;) Since gzip.py lives in python24\lib I thought setting sys.path[0] to python24\lib would load this load this module, no matter what. However, in some magically way the sys.path gets modified during the request by IIS. Maybe IIS resets the global sys.path per new request, causing sporadic problems when request are handled concurrently? I just don't know. The iis/inetsrv path is included in sys.path for a reason and I don't want to fiddle around with sys.path to create problems in other parts of the code. I was wandering if there is some way to prevent a name clashes by using a custom importer or something like that. -- http://mail.python.org/mailman/listinfo/python-list
Re: utcnow
Time zones! So much fun. Looks like you're dealing with UTC offsets
yourself, which gets messy as soon as you start thinking about DST. The
good news is that there's a timezone database on most systems, which you
should almost definitely use.
Take a look at python-dateutil (pip install python-dateutil):
>>> from datetime import datetime
>>> from dateutil import tz
>>> datetime.now()
datetime.datetime(2012, 9, 17, 6, 33, 57, 158555)
This is a so-called "naive datetime", it doesn't know about timezones. On
my system, it returns a time in my local time zone. (It could have been GMT
or something else entirely.) You have to call .replace() on a "naive
datetime" to set the tzinfo member to make it a timezone-aware datetime
object:
>>> AMS = tz.gettz('Europe/Amsterdam')
>>> ATH = tz.gettz('Europe/Athens')
>>> datetime.now().replace(tzinfo=AMS).astimezone(ATH)
datetime.datetime(2012, 9, 17, 7, 37, 38, 573223,
tzinfo=tzfile('/usr/share/zoneinfo/Europe/Athens'))
Voila, it seems like you're one hour ahead of me. :-)
HTH,
Joost
On 17 September 2012 06:25, Nick the Gr33k wrote:
> Hello is there a better way of writing this:
>
> date = ( datetime.datetime.utcnow() + datetime.timedelta(hours=3)
> ).strftime( '%y-%m-%d %H:%M:%S')
>
> something like:
>
> date = datetime.datetime.utcnow(**hours=3).strftime( '%y-%m-%d %H:%M:%S')
>
> i prefer it if it could be written as this.
>
> Also what about dayligh savings time?
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
--
http://mail.python.org/mailman/listinfo/python-list
Splitting large packages with distutils
Hello list, suppose I have three packages like this: ingredients-base/ ingredients/ __init__.py setup.py <-- this one only references package ingredients ingredients-spam/ ingredients/ __init__.py spam/ __init__.py recipe.py setup.py <-- this one only references package ingredients.spam ingredients-eggs/ ingredients/ __init__.py eggs/ __init__.py recipe.py setup.py <-- this one only references package ingredients.eggs Now I can install these like this: virtualenv env env/bin/pip install file:///path/to/ingredients-base env/bin/pip install file:///path/to/ingredients-spam env/bin/pip install file:///path/to/ingredients-eggs Now I have one source tree with the packages ingredients, ingredients.spam and ingredients.eggs all rolled into one, so that works OK. But when I run pip uninstall for ingredients.spam, it also removes the source files for ingredients and ingredients.eggs. That seems a bit wrong, but maybe I'm using distutils/pip/setuptools the wrong way. I found out that if I modify top_level.txt in each of the egg-info directories that are installed so that they include the full package name, pip doesn't uninstall all the code. (And I didn't see any bad effects on the sys.path.) But I haven't found a non-hackish way to make top_level.txt contain the 'right' package name, so I think I'm not supposed to touch it, or even know that it's there. I wasn't able to find much documentation on this top_level.txt file, but what I found suggested that it's used for run-time conflict resolution, not for package management. [1] So my question is, how to make this scenario work? Just use different top-level package names and install them side-by-side? Looking at big projects such as Zope, Django or Twisted, they all seem to have gone the non-distutils route. Before anyone asks why I want this; I want to split up some framework-type code and some utility-type code I have from some application-specific code, and I hope that this way I'll be able to accurately specify and install the dependencies between projects without running ever more risk of clashing with a top-level module name. But I'm open to the suggestion that my idea is totally misguided. :-) Salutation, Joost Molenaar [1] http://svn.python.org/projects/sandbox/trunk/setuptools/doc/formats.txt -- http://mail.python.org/mailman/listinfo/python-list
use a regex or not?
I am looking for a function that takes an input string
and a pattern, and outputs a dictionary.
# @param s str, lowercase letters
# @param p str, lowercase and uppercase letters
# @return dict
def fill(s, p):
d = {}
return d
String s has characters from the lowercase letters.
String p is a pattern, a string of characters from the
lowercase and uppercase letters. The idea is to match
s with p, where lowercase letters have to match
exactly, and to fill variables (with an uppercase
letter name) with the rest of s. The variables are
collected in a dictionary with the resulting bindings.
A variable that occurs in more than one place in p must
bind to the same substring of s.
Tests:
>>> fill('ab', p='aA')
{'A': 'b'}
>>> fill('ab', p='Ab')
{'A': 'a'}
>>> fill('bb', p='Ab') # no match
{}
>>> fill('aa', p='Aa')
{'A': 'a'}
>>> fill('aa', p='Ab') # no match
{}
>>> fill('abb', p='aA')
{'A': 'bb'}
>>> fill('aba', p='aAa')
{'A': 'b'}
>>> fill('abb', p='aAa')# no match
{}
>>> fill('abab', p='aAaA') # A-matches must be equal
{'A': 'b'}
>>> fill('abac', p='aAaA') # no match
{}
>>> fill('abac', p='aAaB')
{'A': 'b', 'B': 'c'}
Can you do it? Is trying a solution with a regex a
good idea?
--
http://mail.python.org/mailman/listinfo/python-list
Re: use a regex or not?
Oops, the 3rd test should be
fill('bb', p='Aa')
resulting in the empty dict {} because no binding for A can be found.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Lisp development with macros faster than Python development?..
Very hard to say. LISP has OOP too, Google for CLOS. Operator overloading is something to avoid anyway, IMHO, just like static typing is something to avoid if you need fast development, on schedule and the like. LISP has one thing that Python does not have: LISP code is LISP data. A thorough study comparing LISP and Python in this respect would be welcome. -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd msg received from list
On Thu, Nov 14, 2013 at 2:53 PM, Verde Denim wrote: > Has anyone else received a message like this? I did too. It seems to me that Gmail's spam filter might have been overly enthusiastic, but the only way to find out is to look at the bounces that the list software received. Yesterday I also received a warning from the debian-laptop mailing list program. Joost -- https://mail.python.org/mailman/listinfo/python-list
Re: Python TUI that will work on DOS/Windows and Unix/Linux
Have you looked at Blessings? I never tried it, but the API seems much cleaner than Curses. https://pypi.python.org/pypi/blessings/ -- Joost Molenaar -- https://mail.python.org/mailman/listinfo/python-list
Re: Tryign to send mail via a python script by using the local MTA
Since the From address is random, it most likely doesn't exist, which could be reason for Google's smtp server to reject the message or to deliver it to spam. Also, the reverse DNS for 84.200.17.58 does not resolve to secure.superhost.gr, which could also be reason to reject the message. On Sun, Sep 15, 2013 at 5:54 PM, Ferrous Cranus wrote: > try: > # prepare mail data > FROM = random_char(10) + '@' + random_char(10) + '.com' > TO = "[email protected]" > > SUBJECT = random_char( 50 ) > MESSAGE = random_char( 500 ) > > os.system( "echo %s | mailx -v -r %s -s %s %s" % (MESSAGE, FROM, SUBJECT, > TO) ) > > print( "%sη αποστολή προς %s επετεύχθη!" % > (times, TO) ) > except Exception as e: > print( "sendmail => ", date, repr( sys.exc_info() ) ) > > sys.exit(0) > > > > > I'am still trying to send successfulyl a mail through my local host by using > the local MTA, trying to avoid using GMail's SMTP server but the mail never > gets send > > this is the error message: > > [code] > [email protected] [~/www/cgi-bin]# python mail.py > LOG: MAIN > cwd=/home/nikos/public_html/cgi-bin 6 args: send-mail -i -v -r > [email protected] [email protected] > LOG: MAIN > <= [email protected] U=nikos P=local S=1052 > id=5235d7f5.bzp0tuy4zqeop7dh%[email protected] > T="QOU0ULMZBF7RGG7B260YERPPXXLTVQ9WKJ93ZXYABQNNA0XB9I" > Content-type: text/html; charset=utf-8 > > 0η αποστολή προς [email protected] > επετεύχθη! > LOG: MAIN > cwd=/var/spool/exim 4 args: /usr/sbin/exim -v -Mc 1VLEdZ-0001Xg-6b > delivering 1VLEdZ-0001Xg-6b > [email protected] [~/www/cgi-bin]# LOG: MAIN > SMTP connection outbound 1379260405 1VLEdZ-0001Xg-6b superhost.gr > [email protected] > Connecting to gmail-smtp-in.l.google.com [173.194.70.26]:25 ... connected > SMTP<< 220 mx.google.com ESMTP e49si15825947eep.141 - gsmtp > SMTP>> EHLO secure.superhost.gr > SMTP<< 250-mx.google.com at your service, [84.200.17.58] > 250-SIZE 35882577 > 250-8BITMIME > 250-STARTTLS > 250-ENHANCEDSTATUSCODES > 250 CHUNKING > SMTP>> STARTTLS > SMTP<< 220 2.0.0 Ready to start TLS > SMTP>> EHLO secure.superhost.gr > SMTP<< 250-mx.google.com at your service, [84.200.17.58] > 250-SIZE 35882577 > 250-8BITMIME > 250-ENHANCEDSTATUSCODES > 250 CHUNKING > SMTP>> MAIL FROM: SIZE=2090 > SMTP<< 250 2.1.0 OK e49si15825947eep.141 - gsmtp > SMTP>> RCPT TO: > SMTP<< 250 2.1.5 OK e49si15825947eep.141 - gsmtp > SMTP>> DATA > SMTP<< 354 Go ahead e49si15825947eep.141 - gsmtp > SMTP>> writing message and terminating "." > SMTP<< 250 2.0.0 OK 1379260407 e49si15825947eep.141 - gsmtp > SMTP>> QUIT > LOG: MAIN > => [email protected] R=lookuphost T=remote_smtp > H=gmail-smtp-in.l.google.com [173.194.70.26] X=TLSv1:RC4-SHA:128 > LOG: MAIN > Completed > [/code] > > > Since all looks okey why the mail never gets delivered? > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Tryign to send mail via a python script by using the local MTA
> Look, > > i want this to stop. > Open your own thread and discuss this if you like. > This is a thread i opened for a specific question and all i see its > irrelevant answers. Hi Ferrous, The problem is not in your Python code. You can debug it from the command line by typing the 'echo ... | mailx' command your Python code is executing. Somewhere on your local SMTP or on Google's SMTP, your message is not being processed the way you want it. There are many things that could cause this -- SPF, DKIM, reverse DNS not being correct, the From address being 'faked', or one of about a hunderd other factors. It's most likely an issue in your local SMTP server's configuration. You'll have to dive into the logs and do some research on how SMTP works nowadays to find out what's happening. But think about this: if I could send mail as [email protected], just by typing a shell command, how could we ever trust e-mail to be from who it really is? What if I send mail as [email protected]? What you're doing may look like spam from Google's point of view, and that's why I think you're not receiving the message. -- https://mail.python.org/mailman/listinfo/python-list
Re: extraction tool using CRF++
Hi Ron,
In the python/ subdirectory of the CRF++ source package there's a
README with instructions on how to use the CRFPP python module.
HTH,
Joost
On Tue, Oct 1, 2013 at 4:24 PM, Vlastimil Brom wrote:
> 2013/10/1 cerr :
>> Hi,
>>
>> I want to write an extraction tool using CRF++
>> (http://crfpp.googlecode.com/svn/trunk/doc/index.html).
>> I have written a trainings file and a template:
>> training:
>> banana FOODB-NP
>> bread FOODI-NP
>> template:
>> U01:%x[0,1]
>> U02:%x[1,1]
>>
>> and now I want to go ahead and extract the foods from a sentence like "how
>> do I make a banana bread". Also, I'm unsure how I interface to crf++ with
>> python, I compiled and installed it from source as described on the above
>> website but I don't have a crf module available in python...
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> Hi,
> I have unfortunately no experience with CRF++; if there is no python
> wrapper for it available, the usage might not be (easily) possible -
> depending on the character of this library, you may try accessing it
> e.g. via ctypes.
>
> Alternatively, you may try another packages already available, e.g.
> NLTK: http://nltk.org/
>
>>>> import nltk
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("apple"))
> True
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("bread"))
> True
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("wine"))
> True
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("book"))
> False
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("pencil"))
> False
>
> # of course there might be some surprise, probably due to polysemy ore
> some specifics of the semantic description...
>
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("dog"))
> True
>>>> any(synset.lexname == "noun.food" for synset in
>>>> nltk.corpus.wordnet.synsets("white"))
> True
>>>>
>
> cf.
> http://nltk.org/
> http://nltk.googlecode.com/svn/trunk/doc/howto/wordnet.html
> http://www.velvetcache.org/2010/03/01/looking-up-words-in-a-dictionary-using-python
> http://wordnet.princeton.edu/man/lexnames.5WN.html
>
> hth,
>vbr
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to streamingly read text file and display whenever updated text
A bit of googling found me this: http://www.linux-support.com/cms/implementation-of-tail-in-python/ import time import sys def tail_f(file): interval = 1.0 while True: where = file.tell() line = file.readline() if not line: time.sleep(interval) file.seek(where) else: yield line -- https://mail.python.org/mailman/listinfo/python-list
Re: The Modernization of Emacs
[Followup-To: header set to comp.emacs] Bjorn Borud wrote: > sure, but often it is just simpler, while you are fiddling around in a > shell, to just fire up vi to do some quick editing than to bounce back > and forth between windows. it is usually quicker too if you have to > navigate deep directory trees -- if you're already in the directory > where the file is, it'd be fewer keystrokes to specify the file than > opening it in emacs. even with tab-completion. well, ok, typing `emacsclient ' requires more keystrokes than `vi ', but that's why i have an alias ec for it... -- Joost Kremers [EMAIL PROTECTED] Selbst in die Unterwelt dringt durch Spalten Licht EN:SiS(9) -- http://mail.python.org/mailman/listinfo/python-list
Re: The Modernization of Emacs: terminology buffer and keybinding
[Followup-To: header set to comp.emacs] blmblm myrealbox.com wrote: > Eclipse has something that generates "import" statements with > a few keystrokes, and for me that's almost in the "killer app > [feature]" class. (Why do I strongly suspect that with the > right plug-ins emacs can do this too? :-) because emacs exposes its lisp system to the user, allowing one to add basically any functionality one can come up with? ;-) -- Joost Kremers [EMAIL PROTECTED] Selbst in die Unterwelt dringt durch Spalten Licht EN:SiS(9) -- http://mail.python.org/mailman/listinfo/python-list
FM synthesis using Numpy
Hello fellow Python coders,
I'm trying to build a simple FM synthesizer in Python. As a beginner,
I take 'FM synthesizer' to
mean: "using a sine wave to control the frequency of another sine wave."
I tried to generate a tone of 1000 Hz that deviates 15 Hz six times a
second. The start of the
resulting wave file sounds right, i.e., a vibrato effect can be heard.
After a second or so, the
vibrato becomes more and more extreme, as if the modulating
oscillator's amplitude is rising
over time. I suspect that I am misunderstanding the math. I tried a
couple of things:
- removing the factor 2 * num.pi from either of the oscillators does
not fix it, besides, doing so is
even more wrong because numpy.sin works with radians
- using a higher sampling frequency makes no difference
- making t run from 0 to 1 each second (t %= 1) causes a clipping of
the sound, so this seems
wrong too
- the problem is not related to Numpy, because the effect also happens
in pure-Python
implementations of my bug
As you can see, I'm at a loss and am even trying incorrect bugfixes.
Any help would be
very welcome.
Thanks for your time,
Joost Molenaar
[I left out a writewavheader function to aid brevity]
---
import numpy as num
def oscillator(x, freq=1, amp=1, base=0, phase=0):
return base + amp * num.sin(2 * num.pi * freq * x + phase)
def writewav(filename, data):
wave = open(filename, 'wb')
# .wav header: 30 s at 44100 Hz, 1 channel of 16 bit signed samples
wave.write('RIFF\x14`(\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D'
'\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\xf0_(\x00')
# write float64 data as signed int16
(32767 * data).astype(num.int16).tofile(wave)
wave.close()
t = num.arange(0, 30, 1./44100)
freq = oscillator(t, freq=6, amp=15, base=1000)
tone = oscillator(t, freq=freq, amp=0.1)
writewav('spam.wav', tone)
---
--
http://mail.python.org/mailman/listinfo/python-list
Re: FM synthesis using Numpy
Thanks/bedankt Bas for the educative reply. I think I got misleaded by Max/MSP's tutorial[1], because MSP seems to automatically adjust the phase when you combine two oscillators in the way that I did. Joost [1] page 112 of http://www.cycling74.com/download/MSP45TutorialsAndTopics.pdf -- http://mail.python.org/mailman/listinfo/python-list
Strange generator problem
Hello all, I bumped into an unexpected problem using generators. Consider this code: def test(s): return _test([], [], s) def _test(s1, s2, s): if s: s1.append(s.pop()) for x in _test(s1, s2, s): yield x s2.append(s1.pop()) for x in _test(s1, s2, s): yield x s.append(s2.pop()) else: yield s1, s2 The test function is supposed be a generator returning all combinations of dividing the elements of the input list into two distinct lists (order of elements unimportant). (This post is not about the quality of the solution. :-) ) Consider now the result of using the generator in a for-loop and in the list function (same result for list comprehension): >>> for x in test(range(3)): ... print x ... ([2, 1, 0], []) ([2, 1], [0]) ([2, 0], [1]) ([2], [1, 0]) ([1, 0], [2]) ([1], [2, 0]) ([0], [2, 1]) ([], [2, 1, 0]) >>> >>> list(test(range(3))) [([], []), ([], []), ([], []), ([], []), ([], []), ([], []), ([], []), ([], [])] The following (simpler) generator works as expected: >>> def test2(s): ... for x in s: ... yield x ... >>> list(test2(range(3))) [0, 1, 2] Can anyone explain the difference? The python version is 2.5.1. Regards, Joost -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange generator problem
On 05/10/2007 11:34, Paul Hankin wrote: > Lists aren't copied when they're yielded, Duh! Thanks for your sharp eye. -- Joost Cassee http://joost.cassee.net -- http://mail.python.org/mailman/listinfo/python-list
Re: The Modernization of Emacs: terminology buffer and keybinding
[EMAIL PROTECTED] wrote: > Don't both "man" and those words for measurement come ultimately from > words for "hand" (similarly to words like "manual", as in labor)? no. "manual" is derived from latin "manus" meaning "hand". the word "man" is related to (though not directly derived from) "mind", and the latin word "mens", which means "mind". -- Joost Kremers [EMAIL PROTECTED] Selbst in die Unterwelt dringt durch Spalten Licht EN:SiS(9) -- http://mail.python.org/mailman/listinfo/python-list
Re: pop langs website ranking
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > Questions: > > • paulgraham.com is unusually high. What's up with that? He writes well. Also, if you look at the ranking, end of januari (Arc's first release) was the highest ranking in 4 months. I'm surprised it didn't score higher, actually. > • python.org at 9k seems also unusally high, compare that perl.org > with online doc and forum is only 26k. Python.org has mailing list > archives... maybe blogs too but am not sure it has forums... still the > gab seems surprising. Even perl is not much talked about these days, > but i'm guessing its market share is at least still 10 or 100 times of > python... Perl has a remarkably fractured web-presence. Have you looked at perl.org? The *only* thing that's really useful on that domain is docs.perl.org. Since python.org apparenly also contains an index to 3rd party libraries, you may want to compare it to cpan.org (currently at 9,199) for instance. -- Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/ -- http://mail.python.org/mailman/listinfo/python-list
Re: optparse/argparse for cgi/wsgi?
Many people have. :-) In the context of WSGI you're basically talking about routing middleware, which solves the problem: "given a request, which application should be called to construct a response?" In my case, it turned out as simple as a list of (regex, resource) tuples, where regex is a regular expression that looks at the request URI and resource is an object that may or may not implement GET, POST, PUT, DELETE methods. In the regex I can capture any arguments that the resource needs. If the request method is GET and the method GET exists on the matched resource, it is called, else a 'Method Not Allowed' response code is returned. HTH Joost On 10 December 2010 17:36, samwyse wrote: > Has anyone ever built some sort of optparse/argparse module for cgi/ > wsgi programs? I can see why a straight port wouldn't work, but a > module that can organize parameter handling for web pages seems like a > good idea, especially if it provided a standard collection of both > client- and server-side validation processes, easy > internationalization, and a way to create customizable help pages. > -- http://mail.python.org/mailman/listinfo/python-list
Re: optparse/argparse for cgi/wsgi?
To aid your googling, the problem is also commonly called 'Dispatching' instead of 'Routing'. Joost On 14 December 2010 12:19, Joost Molenaar wrote: > Many people have. :-) > > In the context of WSGI you're basically talking about routing > middleware, which solves the problem: "given a request, which > application should be called to construct a response?" > > In my case, it turned out as simple as a list of (regex, resource) > tuples, where regex is a regular expression that looks at the > request URI and resource is an object that may or may not > implement GET, POST, PUT, DELETE methods. In the regex > I can capture any arguments that the resource needs. If the > request method is GET and the method GET exists on the > matched resource, it is called, else a 'Method Not Allowed' > response code is returned. > > HTH > > Joost > > > On 10 December 2010 17:36, samwyse wrote: > >> Has anyone ever built some sort of optparse/argparse module for cgi/ >> wsgi programs? I can see why a straight port wouldn't work, but a >> module that can organize parameter handling for web pages seems like a >> good idea, especially if it provided a standard collection of both >> client- and server-side validation processes, easy >> internationalization, and a way to create customizable help pages. >> > -- Joost Molenaar +31 644 015 510 -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
Hi Nico, it's converting fill==True to an int, thereby choosing the
string "False," or "True," by indexing into the tuple.
Try this in an interpreter:
>>> ['a','b'][False]
'a'
>>> ['a','b'][True]
'b'
>>> int(False)
0
>>> int(True)
1
Joost
On 29 September 2010 12:42, Tracubik wrote:
>
> Hi all,
> I'm studying PyGTK tutorial and i've found this strange form:
>
> button = gtk.Button(("False,", "True,")[fill==True])
>
> the label of button is True if fill==True, is False otherwise.
>
> i have googled for this form but i haven't found nothing, so can any of
> you pass me any reference/link to this particular if/then/else form?
>
> thanks
> Nico
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list
Re: how to scrutch a dict()
Using a 2.7/3.x dictionary comprehension, since you don't seem to mind
creating a new dictionary:
def _scrunched(d):
return { key: value for (key, value) in d.items() if value is not None }
Joost
On 21 October 2010 06:32, Phlip wrote:
>
> Not Hyp:
>
> def _scrunch(**dict):
> result = {}
>
> for key, value in dict.items():
> if value is not None: result[key] = value
>
> return result
>
> That says "throw away every item in a dict if the Value is None".
>
> Are there any tighter or smarmier ways to do that? Python does so
> often manage maps better than that...
>
> --
> Phlip
> http://zeekland.zeroplayer.com/
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list
Descriptors and decorators
WebOb contains this little nugget of code that allows you to define a
decorator that works on both methods and functions:
class Decorator(object):
def __init__(self, func):
self.func = func
def __get__(self, object, type=None):
if type is None:
return self
newfunc = self.func.__get__(object, type)
return self.__class__(newfunc)
I adapted it into a class, so that I can inherit this functionality
without thinking about it:
class trace(Decorator):
def __call__(self, *a, **kw):
print '-->', self.func.__name__, repr(a), repr(kw)
result = self.func(*a, **kw)
print '<--', self.func.__name__, '=', repr(result)
return result
I can then use it like this:
class C(object):
@trace
def m(self, x):
return 2 * x
And like this:
@trace
def f(x):
return 2 * x
It works:
>>> o = C()
>>> o.m(21)
--> m (21,) {}
<-- m = 42
>>> f(21)
--> f (21,) {}
<-- f = 42
I'm still not sure what Decorator.__get__ does, or at least I'm not
sure enough to be able to explain it well.
Logically, the method C.m is unbound at the time the class is defined
but o.m is bound when it is called. This means that what
Decorator.__init__ receives as its 'func' parameter is the unbound
method C.m, and when it runs it should operate on the bound method o.m
instead. I suspect that this is what happens inside Decorator.__get__:
create a new instance of the decorator, with a bound version of the
decorated method, and call that without needing a 'self' parameter.
Is this in fact the easiest way to explain it?
Joost Molenaar
--
http://mail.python.org/mailman/listinfo/python-list
Re: Descriptors and decorators
On 25 October 2010 15:20, [email protected] wrote: > So, your decorator is applied to a function, and wraps it into a > Decorator object. Or more exactly, the function is defined, then the > Decorator class is called so a new Decorator object is instanciated > with the function as argument, and finally this Decorator instance is > rebound to the name under which the function was formely known. All > this happenning _before_ class C even exists, so when the class object > is created, it has an attribute by the name of the decorated function > which is in fact a Decorator instance. > > Now this instance is itself a descriptor, so when C.m or o.m are > looked up, the descriptor protocol is fired and Descriptor.__get__ is > called. If called without at least a 'type' argument, it just returns > itself, so it works as an ordinary function. Else it calls the > function's own descriptor implementation to get a bound or unbound > method, and returns a new instance of the decorator with the method as > argument. Thanks, Bruno. Your python-wiki page and walk-through for the Decorator code make it clear. I now finally understand that methods are in fact ordinary functions at the time the class is created, and that the descriptor protocol turns them into bound or unbound methods when they're accessed as attributes: >>> class K(object): pass ... >>> def g(self): pass ... >>> g >>> K.m = g >>> K.m >>> K.__dict__['m'] >>> K().m > Cheers! Now I will try to wrap my brain around metaclasses and coroutines. ;-) -- http://mail.python.org/mailman/listinfo/python-list
