Search posterous

Search all posts and users. Type a name, type a favorite song title, whatever! See what comes up.
  

More posterous blogs











More recommended blogs »

Here are posterous posts filed under python...

mlapida says...

After my little trip to the bookstore today, I decided I would probably need a book on Google App Engine as well, since it does slightly differ from standard python, and the datastore, Google's databases, are very deferent than anything I've worked with in the past (read: relational).

i.e. To use a database in Google app engine, you must write it as a class:

 
class Greeting(db.Model): 
 author = db.UserProperty() 
 content = db.StringProperty(multiline=True) 
 date = db.DateTimeProperty(auto_now_add=True) 

This class goes within the code, and can be written to through statements such as:

 
greeting = Greeting()


if users.get_current_user(): 
 greeting.author = users.get_current_user()


greeting.content = self.request.get('content') 
 greeting.put() 

In this sample from Google, you can see the program checking to see if the user is logged in, requesting the "content" input field from the form and sending the data to the datastore. Much more on all this to come, but for now you can find the standard tutorial on how to do this on Google's own code site. Unfortunately the book is just on preorder; fortunately it is released at the end of this month, so I wont be waiting too long. In the meantime, I'll be catching up on this python.

Filed under: python

Ted says...

Representing dates and times as HTML elements is probably one of the most frustrating parts of web development. My friend and fellow Djangonaut Ben Kreeger has posted an excellent writeup on how to integrate the django.contrib.admin date/time picker widget into your own Django form classes. Check it out here at his blog.

Filed under: python

mlapida says...

Since I really don't know Python all that well, I decided to head over to the local Barnes and Noble to take a look at their selection. There was a few to choose from, but after reading the forwards and looking up some Amazon reviews I settled with this one. I like it becaus it gets to the point. No "this is what 'print' means garbage to sort through first.

Filed under: python

hdknr says...

Filed under: Python

hdknr says...

Filed under: Python

hdknr says...

ぽちる力。

Filed under: Python

sunilarora says...

A friend of mine asked me about the ways a binary search tree (BST henceforth) could be transferred over the network. There are many ways to do so but I found one approach worth discussing here. BST can be serialized by  storing the BST in pre-order or in-order sequence and then wired over the network and then reconstructed by deserializing the sequence. Deserialization from a pre-order sequence is pretty straight forward but deserializing a in-order sequence in to BST is worth a blog post, so I decided to post it here.  I have been spending some time with Python for sometime so decided to give a try (The fact is that I find Python to be really cool).

DISCLAIMER: I am not a Python expert, so please do not expect perfect pythonic code here :)

It is being assumed that an in-order sequence will be given as input to be de-serialized.

Defined a class TreeNode to capture a  properties of tree node.

class TreeNode():
	def __init__(self, val):
	  self.val = val
	  self.left = None
	  self.right = None

We can use famous Divide and Conquer philosophy to solve this problem by building first the left sub-tree, then right-subtree and then combining the two to create the complete tree. Here is the recursive function.

def build_tree(sequence, low, high):
	if low > high:
  		return None
	if low == high:
  		return TreeNode(sequence[low])
	mid = (low + high)/2
	node = TreeNode(sequence[mid])
	node.left = build_tree(sequence, low, mid -1)
	node.right = build_tree(sequence, mid + 1, high)
	return node

Recursive function to print the content of the tree in in-order sequence.

def traverse(node):
	if not node:
	   return
	traverse(node.left)
	print node.val,
	traverse(node.right)

Finally lets test out the code with following inorder sequence. Currently, we do not check if the input sequence if indeed a sorted sequence or not.

if __name__ == "__main__":
 	sequence = [8, 10, 11, 12, 14, 18, 20]
	tree = build_tree(sequence, 0, len(sequence) -1)
	traverse(tree)

Do share your views about it.

Filed under: python

ssk says...

>> :~/src/thrudb/tutorial/py# python BookmarkExample.py
>> Traceback (most recent call last):
>> File "BookmarkExample.py", line 194, in
>> bm.load_tsv_file("../bookmarks.tsv")
>> File "BookmarkExample.py", line 82, in load_tsv_file
>> self.add_bookmark(b)
>> File "BookmarkExample.py", line 89, in add_bookmark
>> bid = self.store_bookmark(b)
>> File "BookmarkExample.py", line 100, in store_bookmark
>> bid = self.thrudoc.putValue(THRUDOC_BUCKET, b_str)
>> File "../gen-py/Thrudoc/Thrudoc.py", line 242, in putValue
>> return self.recv_putValue()
>> File "../gen-py/Thrudoc/Thrudoc.py", line 266, in recv_putValue
>> raise result.e
>> Thrudoc.ttypes.ThrudocException: ThrudocException(what='S3Backend
error', type=1)

S3Backend error になるのでなんか入れないとだめ

Filed under: python

ssk says...

s3 buffer:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Key>td_bookmarks</Key><RequestId>1FFCA809BE735AF4</RequestId><HostId>+xGw8qmEXQ1Ep2q0jHjFbrXHurwp/YNAN8Yxzfvsmkmy45VirPLSv8tdIdmaEekv</HostId></Error>izati�Traceback (most recent call last):
  File "BookmarkExample.py", line 197, in <module>
    bm.remove_all()
  File "BookmarkExample.py", line 125, in remove_all
    for ids in chunker(seed, 100, self.thrudoc.scan):
  File "BookmarkExample.py", line 28, in chunker
    chunk = func(THRUDOC_BUCKET, seed, size)
  File "../gen-py/Thrudoc/Thrudoc.py", line 277, in scan
    return self.recv_scan()
  File "../gen-py/Thrudoc/Thrudoc.py", line 290, in recv_scan
    (fname, mtype, rseqid) = self._iprot.readMessageBegin()
  File "usr/lib/python2.5/site-packages/thrift/protocol/TBinaryProtocol.py", line 126, in readMessageBegin
  File "usr/lib/python2.5/site-packages/thrift/protocol/TBinaryProtocol.py", line 203, in readI32
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 58, in readAll
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 267, in read
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 271, in readFrame
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 58, in readAll
  File "usr/lib/python2.5/site-packages/thrift/transport/TSocket.py", line 94, in read
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

 

エラーが出るが s3 バケットは出来ていた。

新着に載せない
  新しいコメントが投稿されました
  ホーム | モブストライク
>>209
時間短縮目的ぐらいかな…

Filed under: python

Milan says...

The goal is to have a positive match on strings 'Šimánek' and 'šimánek' when doing a case-insensitive comparison. Sounds like an easy task, right? It turns out it's not that easy due to the 'Š/š' national characters at the beginning of the strings. A simple:

>>> re.match(u'šimánek', u'Šimánek', re.I)

returns None. Setting the right locale or using the re.L flag doesn't help either. After a couple of experiments, I found a way how to match these strings:

>>> re.match('šimánek'.decode('utf-8'), 'Šimánek'.decode('utf-8'), re.I | re.U)

<_sre.SRE_Match object at 0x8d82480>

Hope this helps.

Filed under: python