Python'da json verileri json olarak kaydetip kaydedilen verileri liste halinde çekmek...
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
print data['fruits']
# {u'fruits': [u'apple', u'banana', u'orange']}
12 Ocak 2014 Pazar
Python json verileri liste haline çevirmek
Python 'da json loads nedir ?
import json
job = ‘{“sender”: “omgbbqhax”,”message” : “justin timeber ++” }’
mad = json.loads(job)
print mad[‘sender’]
print mad[‘message’]
job = ‘{“sender”: “omgbbqhax”,”message” : “justin timeber ++” }’
mad = json.loads(job)
print mad[‘sender’]
print mad[‘message’]
Python pickle nedir nasıl kullanılır .
import pickle
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
obj = ExampleObject()
pickled_object = pickle.dumps(obj)
r.set('some_key', pickled_object)
unpacked_object = pickle.loads(r.get('some_key'))
obj == unpacked_object
Pymongo Kullanımı örnekleri
#öncelikle pymongoyu import ediyorum ve pymongo clienti çekiyorum.
#3. satırda yeni bir client oluşturdum.
#4. satırda database’e bağlandım database ‘e collection a bağlanmak için kullanacağız ayrıca bazı fonksiyonlarda db işimize yarıyacak.
#collection ile koleksiyonumuzu seçtik bu sql ‘deki tablo anlamına geliyor.
#find_one() ile rastgele sanırım ilk datamızı çekiyor array değişkenlerine bakmak için kullanılabilir tabii içi boş halde istersek içini doldurabiliyor ve özel sorgular yazabiliyoruz.
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.chatsocial
collection = db.channels
collection.find_one()
collection.find_one({“channel”: “victimia”})
#yukarıda channeli victimia olarak kayıtlı json ‘un tüm bilgilerini getiriyor.
data = collection.find_one({“channel”: “victimia”})
#istersek gelen jsonu yukarıdaki gibi bir değişkene atayarak işlem yapabiliyoruz.
Eğer kolleksiyonda değişiklik yapmak istersek :
collection.update({“channel”:”victimia”},{“$set”: {“selfs”: [“deneme”,”dene”]}}, upsert=False)
data = {“channel”:”yasinaktimur”, “selfs”: [“one”,”two”]}
mff = collection.insert(data)
#bu şekilde veri eklenebilir (aynı veri iki kez eklenebilir.)
print mff derseniz eklenen objenin id değerini verir siz id değeri girmesenizde #mongoDB otomatik id atar.
#3. satırda yeni bir client oluşturdum.
#4. satırda database’e bağlandım database ‘e collection a bağlanmak için kullanacağız ayrıca bazı fonksiyonlarda db işimize yarıyacak.
#collection ile koleksiyonumuzu seçtik bu sql ‘deki tablo anlamına geliyor.
#find_one() ile rastgele sanırım ilk datamızı çekiyor array değişkenlerine bakmak için kullanılabilir tabii içi boş halde istersek içini doldurabiliyor ve özel sorgular yazabiliyoruz.
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.chatsocial
collection = db.channels
collection.find_one()
collection.find_one({“channel”: “victimia”})
#yukarıda channeli victimia olarak kayıtlı json ‘un tüm bilgilerini getiriyor.
data = collection.find_one({“channel”: “victimia”})
#istersek gelen jsonu yukarıdaki gibi bir değişkene atayarak işlem yapabiliyoruz.
Eğer kolleksiyonda değişiklik yapmak istersek :
collection.update({“channel”:”victimia”},{“$set”: {“selfs”: [“deneme”,”dene”]}}, upsert=False)
data = {“channel”:”yasinaktimur”, “selfs”: [“one”,”two”]}
mff = collection.insert(data)
#bu şekilde veri eklenebilir (aynı veri iki kez eklenebilir.)
print mff derseniz eklenen objenin id değerini verir siz id değeri girmesenizde #mongoDB otomatik id atar.
Django with SockJS
Few days ago Peter Bengtsson wrote an interesting blog post on SockJS:
The article is oldukça kısa, let me try to provide step-by-step instructions on how to start your first Django on SockJS project.
Currently there are a number of SockJS libraries that work with variety of servers (with quality and completeness varying):
I’ll focus on SockJS-tornado here. This means that the usual Django deployment instructions will not be fully applicable to our project (as we’ll be using Tornado Web HTTP server).
In the mentioned article Peter suggested starting two HTTP servers separately - one for Tornado and one for Django. In this blog post I’ll put Django behind Tornado, so a single Tornado web server will handle all the requests.
We will serve a static file -
Additionally you need to expose the file from
Finally, we need to create the
Put it into a
Finally, visit
The full project is available on github.
If you wish to use this setup on production, you will be able to get better performance by separating Tornado from Django. Django is blocking, Tornado is asynchronous, it makes sense to scale them separately.
The article is oldukça kısa, let me try to provide step-by-step instructions on how to start your first Django on SockJS project.
Python servers
First, it’s important to understand that there are many HTTP (okay, WSGI) servers for Django. SockJS requires pretty deep integration with the web server, and you will need to use a particular web server with SockJS support.Currently there are a number of SockJS libraries that work with variety of servers (with quality and completeness varying):
I’ll focus on SockJS-tornado here. This means that the usual Django deployment instructions will not be fully applicable to our project (as we’ll be using Tornado Web HTTP server).
In the mentioned article Peter suggested starting two HTTP servers separately - one for Tornado and one for Django. In this blog post I’ll put Django behind Tornado, so a single Tornado web server will handle all the requests.
Step 0: Python requirements
We will need few Python packages - Django, Tornado and SockJS-Tornado. Let’s install them into avirtualenv environment:$ mkdir djangosockjs $ cd djangosockjs $ cat > requirements.txt << EOF tornado==2.1.1 sockjs-tornado==0.0.4 django==1.4.1 EOF $ virtualenv venv $ ./venv/bin/pip install -r requirements.txt
Step 1: New project
Let’s create a normal Django project and activate the virtual environment:$ ./venv/bin/django-admin.py startproject project $ . ./venv/bin/activate (venv)$ cd project/project
Step 2: Serving a static file
The Django project will be only a placeholder serving a single static file, without any logic inside. We only want to prove the usage of SockJS with Django using the same codebase and HTTP server.We will serve a static file -
index.html. You need to update TEMPLATE_DIRS in settings.py file:TEMPLATE_DIRS = ( 'project/templates' )
urls.py:from django.conf.urls import patterns, include, url from django.views.generic.simple import direct_to_template urlpatterns = patterns('', url(r'^$', direct_to_template, {'template': 'index.html'}), )
index.html file. For simplicity we’ll borrow a very simple code from SockJS-node examples.(venv)$ mkdir templates (venv)$ cd templates (venv)$ wget https://raw.github.com/sockjs/sockjs-node/master/examples/echo/index.html (venv)$ cd .. (venv)$ cd ..
Step 3: Tornado code
Our SockJS code will accept any incoming realtime connections and will echo all the received data. Here’s the code for theproject/echosockjs.py file:import sockjs.tornado class EchoSockjsConnection(sockjs.tornado.SockJSConnection): def on_open(self, request): print "sockjs: open" def on_message(self, data): print "data: %r" % (data,) self.send(data) def on_close(self): print "sockjs: close" def EchoSockjsRouter(prefix): return sockjs.tornado.SockJSRouter(EchoSockjsConnection, prefix).urls
Step 4: Tornado server
The last file we need to write, is the Tornado web server wrapper. It will do two things:- it will expose our SockJS endpoint
EchoSockjsConnectionunder/echopath - it will forward all other requests to normal Django app
#!/usr/bin/env python from tornado.options import options, define import django.core.handlers.wsgi import tornado.httpserver, tornado.ioloop import tornado.web, tornado.wsgi import project.echosockjs define('port', type=int, default=8000) wsgi_app = tornado.wsgi.WSGIContainer( django.core.handlers.wsgi.WSGIHandler()) tornado_app = tornado.web.Application( project.echosockjs.EchoSockjsRouter('/echo') + [ ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)), ]) server = tornado.httpserver.HTTPServer(tornado_app) server.listen(options.port) print "[*] Listening at 0.0.0.0:%i" % (options.port,) tornado.ioloop.IOLoop.instance().start()
tornado_main.py file.Finito
And to start the server:(venv)$ chmod +x tornado_main.py (venv)$ DJANGO_SETTINGS_MODULE=project.settings ./tornado_main.py
http://localhost:8000/ and see if the echo is indeed working!The full project is available on github.
If you wish to use this setup on production, you will be able to get better performance by separating Tornado from Django. Django is blocking, Tornado is asynchronous, it makes sense to scale them separately.
26 Aralık 2013 Perşembe
Kaydol:
Kayıtlar (Atom)