I am trying to share an object between two GAE apps. The first will have the class's file, and will offer up an instance of that object. The second, using a given url, will access the first app, get the object and then use is. Is this actually possible? If so what am I not doing right in the code below?
As a small side note I tried a solution with pickle, but both apps are required to have the class in its name space, but I will be working with a number of these. I thought about trying to imitate something like Java's abstract class by using inheritance, but that didn't work out. I can provide that code too if you want to see it.
I understand the possible Terms of Service, that is not a issue.
I know cloud computing is out there, I don't know how to work with it, and I would
prefer to avoid the costs because I am developing this as a class project.
I have seen some suggestions to use remote_api, but I have seen no good example
of how it can be used, let alone used to allow two applications to interact.
I have seen the solution to use multiple versions, but each student will have
an app, it would be incredibly messy, but possibly doable.
First.Py:
class SampleCritter():
def move():
...
class Access(webapp2.RequestHandler):
def post(self):
CritStore(stats=self.request.body).put()
def get(self):
creature = CritStore.all().order('-date').get()
if creature:
stats = loads(creature.stats)
return SampleCritter(stats)
else:
return SampleCritter()
Second.py:
class Out(webapp2.RequestHandler):
def post(self):
url = self.request.POST['url']
critter = urllib2.urlopen(url)
critter.move()
The short answer is, you can't share objects between apps.
The longer answer is, your first app can expose objects using an HTTP based API. Any client can access the HTTP API, including app 2.
App 2 will have to manipulate objects via the HTTP API. You won't be able to call critter.move() from app 2, though if you create a handler say, critter\move, you can have the handler pull up the appropriate Critter instance and call move() on it. You'll have to pass all the appropriate params via HTTP POST as well.
Related
I have a pyramid API which has basically three layers.
View -> validates the request and response
Controller -> Does business logic and retrieves things from the DB.
Services -> Makes calls to external third party services.
The services are a class for each external API which will have things like authentication data. This should be a class attribute as it does not change per instance. However, I cannot work out how to make it a class attribute.
Instead I extract the settings in the view request.registry.settings pass it to the controller which then passes it down in the init() for the service. This seems unnecessary.
Obviously I could hard code them in code but that's an awful idea.
Is there a better way?
Pyramid itself does not use global variables, which is what you are asking for when you ask for settings to be available in class-level or module-level attributes. For instance-level stuff, you can just pass the settings from Pyramid into the instance either from the view or from the config.
To get around this, you can always pass data into your models at config-time for your Pyramid app. For example, in your main just pull settings = config.get_settings() and pass some of them to where they need to be. As a general rule, you want to try to pass things around at config-time once, instead of from the view layer all the time.
Finally, a good way to do that without using class-level or module-level attributes is to register instances of your services with your app. pyramid_services library provides one approach to this, but the idea is basically to instantiate an instance of a service for your app, add it to your pyramid registry config.registry.foo = ... and when you do that you can pass in the settings. Later in your view code you can grab the service from there using request.registry.foo and it's already setup for you!
I'm trying to unit test Django REST Framework view set permissions for two reasons: speed and simplicity. In keeping with these goals I would also like to avoid using any mocking frameworks. Basically I want to do something like this:
request = APIRequestFactory().post(…)
view = MyViewSet.as_view(actions={"post": "create"})
self.assertTrue(MyPermission().has_permission(request, view))
The problem with this approach is that view is not actually a View instance but rather a function which does something with a View instance, and it does not have certain properties which I use in has_permission, such as action. How do I construct the kind of View instance which can be passed to has_permission?
The permission is already tested at both the integration and acceptance level, but I would like to avoid creating several complex and time-consuming tests to simply check that each of the relevant actions are protected.
I've been able to work around this by monkeypatching a view set instance and manually dispatching it:
view_set = MyViewSet()
view_set.action_map = {"post": "create"}
view_set.dispatch(request)
You can do something like below.
request = APIRequestFactory().post(…)
view_obj = MyViewSet()
self.assertTrue(MyPermission().has_permission(request, view_obj))
I have built flask apis in the past, but I've never had the luxury of being on a project where I was allowed to do test driven development while working on one. Now I am.
Every how-to I've ever read shows flask-apis being defined in a decidedly non-object-oriented style. This makes writing unit tests very difficult because one cannot pass optional or test-specific parameters into the instantiated api.
For example, this blog explains one method for creating unit tests for a flask-api:
http://mkelsey.com/2013/05/15/test-driven-development-of-a-flask-api/
The blog sets an environment variable when in test mode and the script watches for that variable, altering its behavior when it is found. This seems very inadvisable to me. There are a long list of reasons why letting your source code bleed into your OS environment is a Bad Idea. I'm a big believer in encapsulating functionality in order to maximize portability and to draw clear distinctions between where modules begin and end.
In order to satisfy my coding philosophy someone might suggest I just wrap the flask API up in a class. However, it isn't clear to me how one would do such a thing. For example, Flask uses decorators in order to define a route:
#app.route()
How could this be fit into a class?
I would greatly appreciate the guidance of anyone who has developed a flask api in an object oriented manner.
You can replace the use of #app.route with add_url_rule.
To put it in an example:
from flask import Flask
class MyFlaskApp:
def __init__(self):
self.app = Flask(__name__)
self.app.add_url_rule('/', 'index', self.index)
def index(self):
pass
Which is similar to:
from flask import Flask
app = Flask(__name__)
#app.route('/index')
def index():
pass
There is very little difference in my get and post methods. One way to do this would be to put the common logic in another function and call that in both the get and post methods. But before I do that I wanted to know If I can actually have one function handle both, that'll be really neat.
You can also use a BaseHandler for your handlers. You can put common methods for sessions, login and templates in the BaseHandler.
See this example for sessions or this blog post about webapp2 and templates.
This is a good description of when to use GET vs POST. You can use either, of course, but there are situations where you'd want to use one vs the other. You can use the same methods to process them from within the same class if you wanted to like this:
class MyHandler(webapp2.RequestHandler):
def function_to_handle_requests(self):
# code goes here
def get(self):
self.function_to_handle_requests
def post(self):
self.function_to_handle_requests
is there any class to handle a SESSION (like php) in Python? not in django, but I want to use it with PyQt
thank you
The short answer is that there is no $SESSION variable in Python.
Python tends not to put things in global scope like PHP. Therefore, if you are accessing a user's session id, it will probably be accessed via dot notation module_name.ClassName.session. If you would like to create a PyQt app that acts as a webserver, you could probably adapt a web framework's implementation.
Others' responses to similar queries suggest implementing sessions via a simple database[1]. You could try assigning unique ids with uuid, and storing them with tools like sqlite3 or pickle.
[1] http://ubuntuforums.org/showthread.php?t=859645
Theres a session class with mod_python for apache, that can be a good starting point for making our own class. The class is not very dependent on apache to work.
http://www.modpython.org/