[Tutor] Still Trying to Understand GAE
Hi All, i've started earning python sone months ago (on Google App Engine unfortunately). I have some doubts reagrding "import", and have asked a similar question here months ago, but without finding a solution. So: with import i can import modules or single functions. And this is ok. Then: as i have understood from all the books i readm in each package directory i have the __init__.py file that decides what import with it. In other words if my package skel is like: /gg/ /gg/sub1/ /gg/sub1/file.py /gg/sub2/ /gg/sub2/file.py and i use "import gg", nothing is imported. To import sub1 and sub2, i can: - Put in /gg/ a __init__.py file that tells to import them - Use "from gg import sub1" Ok now the $1 Billion question: google app engine has the same schema than my "gg" package, an empty __init__.py file, but if i use "import google" it also imports all subdirectories. And i can't understand wiìhy it does so. Can you help me? Thankyou Giorgio ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still Trying to Understand GAE
Hi Wesley! Thankyou very much for your mail (and i'd like to thankyou in a special way for your corrections :) ). > it imports all sub*packages*. don't think directories because the > import mechanism doesn't work this way. this is likely because there > are __init__.py files in those subdirectories. another possibility is > that there are non-empty __init__.py files that do the imports of > things that you're not expecting. Ok, i've checked all those files, because in a standard python evinronment the __init__.py is the only file that can decide what to import. If you want to check directly, here you can find the SDK. Just open the "google" directory. http://googleappengine.googlecode.com/files/google_appengine_1.2.5.zip As i've said, this simple script: --- #!/usr/bin/python import google print "Content-Type: text/html" print "" print "" print "" print "" print "google",dir(google) print "" print "" print "" --- Gives this output: "google ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'appengine', 'net', 'pyglib']" The "google" directoy has an empty __init__.py file (well, if we want to be completely correct it contains some commented -#- lines). Same for "appengine", "net" and "pyglib". As they all have an __init__.py file, they should be consiedered as modules from the python interpreter. So, if i run "import google" it imports all google's submodules. Well, now, the problem: if i create another directory (module) in the google dir, it doesn't get imported. Of course i've put in it some .py files and an empty __init__.py file. Thankyou again! Giorgio 2009/9/13 wesley chun : > hi Giorgio, > > welcome to Python (whether directly or from GAE!) :-) my comments below. > > >> with import i can import modules or single functions. And this is ok. > > not quite true. regardless of whether you use import or from-import, > you're *always* importing (and loading) modules or packages in their > entirety. > > now, whether you have *access* to "entire" modules/packages or > individual attributes (functions, classes, or standard data), is > another matter -- usually this is a result of using from-import. > > also, the difference between importing and loading is that loading > only happens the first time you import a module/package. (if you do it > more than once, e.g., module A imports B and C and module B also > imports C, the import of C happens twice but the loading happens only > once. > > >> if i use "import >> google" it also imports all subdirectories. And i can't understand >> wiìhy it does so. > > it imports all sub*packages*. don't think directories because the > import mechanism doesn't work this way. this is likely because there > are __init__.py files in those subdirectories. another possibility is > that there are non-empty __init__.py files that do the imports of > things that you're not expecting. > > hope this helps! > -- wesley > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 > "Python Fundamentals", Prentice Hall, (c)2009 > http://corepython.com > > wesley.j.chun :: wescpy-at-gmail.com > python training and technical consulting > cyberweb.consulting : silicon valley, ca > http://cyberwebconsulting.com > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still Trying to Understand GAE
Thankyou all, you're very precious for me. yeah it seems the development webserver (and the production one) are importing modules in a non-standard way. I absolutely don't understand this choice. Why import everything everytime? Don't you think it makes scripts much more slow? Giorgio 2009/9/16 Kent Johnson : > On Sun, Sep 13, 2009 at 9:59 AM, ad...@gg-lab.net wrote: >> Hi All, >> >> i've started earning python sone months ago (on Google App Engine >> unfortunately). >> >> I have some doubts reagrding "import", and have asked a similar >> question here months ago, but without finding a solution. >> >> So: >> >> with import i can import modules or single functions. And this is ok. >> Then: as i have understood from all the books i readm in each package >> directory i have the __init__.py file that decides what import with >> it. In other words if my package skel is like: >> >> /gg/ >> /gg/sub1/ >> /gg/sub1/file.py >> /gg/sub2/ >> /gg/sub2/file.py >> >> and i use "import gg", nothing is imported. To import sub1 and sub2, i can: >> >> - Put in /gg/ a __init__.py file that tells to import them >> - Use "from gg import sub1" >> >> Ok now the $1 Billion question: google app engine has the same schema >> than my "gg" package, an empty __init__.py file, but if i use "import >> google" it also imports all subdirectories. And i can't understand >> wiìhy it does so. > > In general, > import foo > does not import subpackages of foo unless they are specifically > imported in foo/__init__.py, so dir(foo) will not show the > subpackages. > > However if you > import foo > import foo.bar > then dir(foo) will include 'bar'. Here is an example from the std lib: > > In [1]: import distutils > > In [2]: dir(distutils) > Out[2]: > ['__builtins__', > '__doc__', > '__file__', > '__name__', > '__package__', > '__path__', > '__revision__', > '__version__'] > > In [3]: import distutils.cmd > > In [4]: dir(distutils) > Out[4]: > ['__builtins__', > '__doc__', > '__file__', > '__name__', > '__package__', > '__path__', > '__revision__', > '__version__', > 'archive_util', > 'cmd', > 'dep_util', > 'dir_util', > 'errors', > 'file_util', > 'log', > 'spawn', > 'util'] > > My guess is that the startup for GAE is importing the subpackages so > they then appear as imported modules. To access your sub-package, just > import it normally. > > Kent > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Still Trying to Understand GAE
Yes Kent, i'm not worrying about it, i was just trying to find the reason why they're doing so. Anyway, i'm a newbye, but the GAE Evinronment is very very difficult to understand. The only thing is thas in common with the real python is the sintax. Thankyou again 2009/9/17 Kent Johnson : > On Thu, Sep 17, 2009 at 8:38 AM, ad...@gg-lab.net wrote: >> Thankyou all, you're very precious for me. >> >> yeah it seems the development webserver (and the production one) are >> importing modules in a non-standard way. >> >> I absolutely don't understand this choice. Why import everything >> everytime? Don't you think it makes scripts much more slow? > > My guess is that they are importing what they need. It does impact > startup but hey, if you need it, you need it. > > Try this for comparison: Start Python from a command line, then > In [5]: import sys > > In [6]: len(sys.modules) > Out[6]: 323 > > I have IPython loaded so this number may be larger than yours. In > Python 3, with no IPython, I get >>>> import sys >>>> len(sys.modules) > 47 > > So my advice is, don't worry about it. > > Kent > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Determine Filetype
Hi, i'm putting file in a DB as BLOB entries. To serve them, i need to take Content-Type headers. So, i'm looking for a function that returnes the filetype, given a data str. I've found many other topics like this in python mail-archive, but any of them contains the solution. Can you help me, please? Thankyou! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Hi Emile, that functions requires a filename/path. Just like this one (for images) http://docs.python.org/library/imghdr.html Ok, i don't have a filename. I get the file from a BLOB in a db. Any idea? Thankyou for your precious help. 2009/9/18 Emile van Sebille : > On 9/18/2009 10:05 AM ad...@gg-lab.net said... >> >> Hi, >> >> i'm putting file in a DB as BLOB entries. To serve them, i need to >> take Content-Type headers. >> >> So, i'm looking for a function that returnes the filetype, given a data >> str. >> >> I've found many other topics like this in python mail-archive, but any >> of them contains the solution. >> >> Can you help me, please? > > I'd take a look at python-magic at > http://hupp.org/adam/hg/python-magic/file/d3cd83e5a773 where the example > shows that you can do: > > # For MIME types >>>> mime = magic.Magic(mime=True) >>>> mime.from_file("testdata/test.pdf") > 'application/pdf' > > > HTH, > > Emile > > > > > >> >> Thankyou! >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Oh, i'm sorry. I've read the README, but haven't noticed that m.from_buffer(open("testdata/test.pdf").read(1024)) was exactly what i was looking for. Ok, i'll try it and let you know :D 2009/9/18 Kent Johnson : > On Fri, Sep 18, 2009 at 2:21 PM, ad...@gg-lab.net wrote: >> Hi Emile, >> >> that functions requires a filename/path. > > Did you even look at the link? There is a from_buffer() method also. > > Kent > >> 2009/9/18 Emile van Sebille : > >>> I'd take a look at python-magic at >>> http://hupp.org/adam/hg/python-magic/file/d3cd83e5a773 where the example >>> shows that you can do: >>> >>> # For MIME types >>>>>> mime = magic.Magic(mime=True) >>>>>> mime.from_file("testdata/test.pdf") >>> 'application/pdf' > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Ok, a good news for me: i've modified my script, adding a: import magic line at the top of it. But I got this error: No module named magic Ok, so magic is not installed on GAE. I've then uploaded it and it loaded succesfully. New error: No module named _ctypes And, reading the full debug i got this: File "/base/python_dist/lib/python2.5/ctypes/__init__.py", line 10, in from _ctypes import Union, Structure, Array So, the __init__.py file of the GAE evinronment's ctypes library is broken, as it's importing from a package that doesn't exist. Right? Maybe i'm doing something wrong. Thankyou 2009/9/18 ad...@gg-lab.net : > Oh, i'm sorry. > > I've read the README, but haven't noticed that > > m.from_buffer(open("testdata/test.pdf").read(1024)) > > was exactly what i was looking for. > > Ok, i'll try it and let you know :D > > 2009/9/18 Kent Johnson : >> On Fri, Sep 18, 2009 at 2:21 PM, ad...@gg-lab.net wrote: >>> Hi Emile, >>> >>> that functions requires a filename/path. >> >> Did you even look at the link? There is a from_buffer() method also. >> >> Kent >> >>> 2009/9/18 Emile van Sebille : >> >>>> I'd take a look at python-magic at >>>> http://hupp.org/adam/hg/python-magic/file/d3cd83e5a773 where the example >>>> shows that you can do: >>>> >>>> # For MIME types >>>>>>> mime = magic.Magic(mime=True) >>>>>>> mime.from_file("testdata/test.pdf") >>>> 'application/pdf' >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Hi All, the solution was in the link i've posted before. If i use: type = imghdr.what('img.test', image.data) The imghdr modules ignore the filename (first argument) and takes the second as a data stream assumed to contain the filetype. The solution only works for images, but that's what i need. I think that right now is not possible to generally determine the mimetype for all types of files on GAE, because some libraries are missing. Anyway, i'll send a mail to the gae mailing list, and if find a solution i'll post it here. Thankyou all guys. 2009/9/19 Sander Sweers : > On Fri, 2009-09-18 at 16:48 -0400, Kent Johnson wrote: >> > So, the __init__.py file of the GAE evinronment's ctypes library is >> > broken, as it's importing from a package that doesn't exist. Right? >> >> Probably ctypes is not supported in GAE, that would be a pretty big >> security hole. > > There is a pure python implementation on pypi [1]. It states it is in > alpha and missing some features. Never used it myself though... > > Greets > Sander > > [1] http://pypi.python.org/pypi/pymagic/0.1 > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Ops, i have another question, it seems very simple. I want to check the extension of an uploaded file. So i've created a list with allowed extensions: enabled_ext = ['gif', 'jpeg', 'png', 'bmp', 'tiff'] How can i create a simple if that check if the file ext (file_ext) is in the list? I've searched in the python tutorial, but have not been able to find any example that uses an if statement combined with a list. Thankyou again 2009/9/19 ad...@gg-lab.net : > Hi All, > > the solution was in the link i've posted before. If i use: > > type = imghdr.what('img.test', image.data) > > The imghdr modules ignore the filename (first argument) and takes the > second as a data stream assumed to contain the filetype. > > The solution only works for images, but that's what i need. I think > that right now is not possible to generally determine the mimetype for > all types of files on GAE, because some libraries are missing. Anyway, > i'll send a mail to the gae mailing list, and if find a solution i'll > post it here. > > Thankyou all guys. > > 2009/9/19 Sander Sweers : >> On Fri, 2009-09-18 at 16:48 -0400, Kent Johnson wrote: >>> > So, the __init__.py file of the GAE evinronment's ctypes library is >>> > broken, as it's importing from a package that doesn't exist. Right? >>> >>> Probably ctypes is not supported in GAE, that would be a pretty big >>> security hole. >> >> There is a pure python implementation on pypi [1]. It states it is in >> alpha and missing some features. Never used it myself though... >> >> Greets >> Sander >> >> [1] http://pypi.python.org/pypi/pymagic/0.1 >> >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
if file_ext in enabled_ext Found an example some seconds ago in another mailing lists. I'm not able to find the python tutorial that describes this statement. Can you help me please? 2009/9/19 ad...@gg-lab.net : > Ops, i have another question, it seems very simple. > > I want to check the extension of an uploaded file. So i've created a > list with allowed extensions: > > enabled_ext = ['gif', 'jpeg', 'png', 'bmp', 'tiff'] > > How can i create a simple if that check if the file ext (file_ext) is > in the list? > > I've searched in the python tutorial, but have not been able to find > any example that uses an if statement combined with a list. > > Thankyou again > > 2009/9/19 ad...@gg-lab.net : >> Hi All, >> >> the solution was in the link i've posted before. If i use: >> >> type = imghdr.what('img.test', image.data) >> >> The imghdr modules ignore the filename (first argument) and takes the >> second as a data stream assumed to contain the filetype. >> >> The solution only works for images, but that's what i need. I think >> that right now is not possible to generally determine the mimetype for >> all types of files on GAE, because some libraries are missing. Anyway, >> i'll send a mail to the gae mailing list, and if find a solution i'll >> post it here. >> >> Thankyou all guys. >> >> 2009/9/19 Sander Sweers : >>> On Fri, 2009-09-18 at 16:48 -0400, Kent Johnson wrote: >>>> > So, the __init__.py file of the GAE evinronment's ctypes library is >>>> > broken, as it's importing from a package that doesn't exist. Right? >>>> >>>> Probably ctypes is not supported in GAE, that would be a pretty big >>>> security hole. >>> >>> There is a pure python implementation on pypi [1]. It states it is in >>> alpha and missing some features. Never used it myself though... >>> >>> Greets >>> Sander >>> >>> [1] http://pypi.python.org/pypi/pymagic/0.1 >>> >>> ___ >>> Tutor maillist - tu...@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
2009/9/19 Sander Sweers : > On Sat, 2009-09-19 at 17:20 +0200, ad...@gg-lab.net wrote: >> I want to check the extension of an uploaded file. So i've created a >> list with allowed extensions: >> >> enabled_ext = ['gif', 'jpeg', 'png', 'bmp', 'tiff'] > > If this does not change make it s tuple. It does not change what is > written below. > >> How can i create a simple if that check if the file ext (file_ext) is >> in the list? > > The below idle session should help you figure it out. > >>>> enabled_ext = ('gif', 'jpeg', 'png', 'bmp', 'tiff') >>>> 'gif' in enabled_ext > True > > Greets > Sander > > A: Because it messes up the order in which people normally read text. > Q: Why is top-posting such a bad thing? > A: Top-posting. > Q: What is the most annoying thing in e-mail? > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Sander, sorry for the top-posting, it's the default in GMail. This is one of my first times in a mailing list, so it's possible i'm doing something wrong. Sorry again for it. I've replied to my mail, as i've found what i was looking for: if OBJECT in LIST: Well, but i'd like to read the page of the python tutorial regarding this. I'm not able to locate it. Can you help me? Thankyou ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
Thankyou guys. > The search feature of the web site is quite effective! :-) Alan, i know that every site has a search function, but they're not so simple to be used if you're looking for a very common word ("in") and don't know other keys to find it. Regards, Giorgio ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor