[google-appengine] Defered function that can take lambdas, closures

2014-10-03 Thread Emlyn
Hi all,

This might be interesting to anyone who uses deferred.defer in python
on appengine.

I've just written this code that expands what you can do with defer.
You can defer a lambda, and you can defer (functions with) closures.
It's implemented using the original defer, but marshals a function's
code across the defer boundary (using the marshal library and a
wrapper function) and keeps the function's closure information intact.

Note that this wont take an arbitrary callable, you need to be passing
a function.

I'd love any feedback; am I doing anything disastrous? Could anything
be done a better way?

import marshal, types, sys
from google.appengine.ext import deferred

# defer any function object to be run in a background task
def Defer(aFunction, *args, **kwargs):

if not aFunction or not hasattr(aFunction, func_code):
raise Exception (First argument required, must be a function)

# recursively turn a function's closure info into something marshallable
def MarshalClosureValues(aClosure):
logging.debug(aClosure)
lmarshalledClosureValues = []
if aClosure:
lclosureValues = [lcell.cell_contents for lcell in aClosure]
logging.debug(lclosureValues: %s % lclosureValues)
lmarshalledClosureValues = [
[marshal.dumps(litem.func_code),
MarshalClosureValues(litem.func_closure)] if hasattr(litem,
func_code)
else [marshal.dumps(litem)]
for litem in lclosureValues
]
return lmarshalledClosureValues

# marshall the function's code
lmarshalledFunc = marshal.dumps(aFunction.func_code)

# marshall the function's closure info
lmarshalledClosureValues = MarshalClosureValues(aFunction.func_closure)

# also grab the function's module (for restoring appropriate globals)
lmoduleName = aFunction.__module__

# call defer on our wrapper function, with all this info;
# it will reverse the above and call the origin function
deferred.defer(WrapDeferred, lmarshalledFunc,
lmarshalledClosureValues, lmoduleName, *args, **kwargs)


# Reverse the marshalling process and call reconstituted function
def WrapDeferred(aMarshalledFunc, aMarshalledClosureValues,
aModuleName, *args, **kwargs):

# get appropriate globals. These are being used for functions
inside closure info
# as well as top level function; is this dangerous?
lglobals = sys.modules[aModuleName].__dict__

# Creates a cell for a value by wrapping it in a function and
then pulling it out
# of the closure info
def make_cell(value):
return (lambda x: lambda: x)(value).func_closure[0]

# Reverse marshalling process on closure info
def UnmarshalClosureValues(aMarshalledClosureValues):
lclosure = None
if aMarshalledClosureValues:
lclosureValues = [
marshal.loads(item[0]) if len(item) == 1
else types.FunctionType(marshal.loads(item[0]),
lglobals, closure=UnmarshalClosureValues(item[1]))
for item in aMarshalledClosureValues if len(item)
= 1 and len(item) = 2
]
lclosure = tuple([make_cell(lvalue) for lvalue in lclosureValues])
return lclosure

# unmarshal the function code
lfunctionCode = marshal.loads(aMarshalledFunc)

# unmarshal the closure info
lclosure = UnmarshalClosureValues(aMarshalledClosureValues)

# create a new function using the unmarshalled code, closure info,
and globals
lfunction = types.FunctionType(lfunctionCode, lglobals, closure=lclosure)

# call the function!
lfunction(*args, **kwargs)



-- 
Emlyn

http://point7.wordpress.com - My blog
https://plus.google.com/u/0/100281903174934656260 - Google+

-- 
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-appengine+unsubscr...@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/d/optout.


Re: [google-appengine] Help Needed in handling Forms (GAE PHP)

2014-10-03 Thread Anmol Parashar
I have done everything. From exporting the files to uploading them on the 
App Engine. The only thing I can't figure out is how to upload the 8 .php 
files. I moved them to a different folder but the *upload: *directive 
didn't work. Can you please give me exact commands to write in my *app.yaml* 
for putting them in the root? 

On Friday, October 3, 2014 3:27:39 AM UTC+5:30, Vinny P wrote:


 On Thu, Oct 2, 2014 at 6:34 AM, Anmol Parashar 4nm...@gmail.com 
 javascript: wrote:


   I have two forms on my website, one asks for just an email and other a 
 contact form. Both of them accepts input but can't send the input to where 
 they are supposed to. A message is displayed, PHP scripts/files are either 
 not configured properly or are missing. Contact your host.

 I made my website using Adobe Muse and their forms uses PHP. I have 
 provided all my static html files to Google App Engine (which I'm using as 
 a host) using the Deploy function but whenever I hit deploy, most of the 
 files are read and uploaded except the 8 .php files. The error says, 
 *Mimetype 
 can't be recognized for *.php. Using application octet stream instead.*

 *How do I upload my .php files?*

   
  
  
 Are you deploying within Adobe Muse, or deploying using the App Engine SDK 
 outside of Muse? If you're attempting to deploy while inside of Muse, try 
 this: open your project in Muse, then go to File  Export HTML and save a 
 copy of your website on your local disk. Then download and install the App 
 Engine PHP SDK ( https://cloud.google.com/appengine/downloads ) and use 
 it to upload your local website copy to App Engine. Here are uploading 
 directions: 
 https://cloud.google.com/appengine/docs/php/gettingstarted/uploading
  
  
 On Thu, Oct 2, 2014 at 6:34 AM, Anmol Parashar 4nm...@gmail.com 
 javascript: wrote:


   - url: /unsubscribe
 script: static_website/jl3sxnhv7vv.php

   
  
 I see that your PHP scripts are in your static files directory. You need 
 to move them into their own directory or put them at root.
  
  
 On Thu, Oct 2, 2014 at 6:34 AM, Anmol Parashar 4nm...@gmail.com 
 javascript: wrote:


*PS:* The support at Adobe Muse says that PHP is not configured. 
 Contact the server admin. How do I tell Google To enable php?

  
  
 PHP is already configured in App Engine, you don't need to do anything 
 special.
  
  
 -
 -Vinny P
 Technology  Media Consultant
 Chicago, IL

 App Engine Code Samples: http://www.learntogoogleit.com
  


-- 
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-appengine+unsubscr...@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/d/optout.


Re: [google-appengine] Help Needed in handling Forms (GAE PHP)

2014-10-03 Thread Anmol Parashar


  
  
 On Thu, Oct 2, 2014 at 6:34 AM, Anmol Parashar 4nm...@gmail.com 
 javascript: wrote:


   - url: /unsubscribe
 script: static_website/jl3sxnhv7vv.php

   
  
 I see that your PHP scripts are in your static files directory. You need 
 to move them into their own directory or put them at root.
  
  

  I tried to put the php scripts in the root folder and changed my *app.yaml 
*to 

application: subscribershop
 version: 1
 runtime: php
 api_version: 1
 threadsafe: yes

 handlers:
 - url: /favicon\.ico
   static_files: favicon.ico
   upload: favicon\.ico

 #root
 - url: /
   script: static_website/php

 #serve our home page in case index.html is requested
 - url: /index.html
   static_files: static_website/index.html
   upload: static_website/index.html



Now when I tried to *Browse* the app using my localhost, it gives me an 
error and says,


*Warning*: require(G:\Anmol\subscribershop\static_website\php): failed to 
open stream: Permission denied in *C:\Program Files 
(x86)\Google\google_appengine\google\appengine\tools\devappserver2\php\setup.php*
 
on line *106*

*Fatal error*: require(): Failed opening required 
'G:\Anmol\subscribershop\static_website\php' 
(include_path='G:\Anmol\subscribershop;C:\Program Files 
(x86)\Google\google_appengine\php\sdk') in *C:\Program Files 
(x86)\Google\google_appengine\google\appengine\tools\devappserver2\php\setup.php*
 
on line *106*
  

-- 
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-appengine+unsubscr...@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/d/optout.