Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

2011-04-22

Use GitPython to see what's going in git

Hi, it's NISHIO Hirokazu. I'm planning to make a git cheat sheet for beginners. I want to see what's going in git, and GitPython tells it to me! Now we can see the change of index, HEAD, branches, tags, objects and their contents. What's else?


# easy_install GitPython

http://packages.python.org/GitPython/0.3.1/intro.html#getting-started


>>> from git import *
>>> r = Repo(".")
>>> r.index.entries
{('README', 0): (100644, 432a4b6de3c8318e1e59178492608cc10e48b00a, 0, README)}


https://github.com/gitpython-developers/gitdb/blob/master/doc/source/tutorial.rst

>>> for k in r.odb.sha_iter():
... r.odb.info(k)
...
('"b\xde\x0c\x12\x1f"\xdf\x8ex\xf5\xa3}n\x11O\xd3"\xc0\xb0', 'blob', 5)
('C*Km\xe3\xc81\x8e\x1eY\x17\x84\x92`\x8c\xc1\x0eH\xb0\n', 'blob', 10)
("I'T\x08\xd5T\xd5[\xaa\xd5\x99\xe5\xb4\x19\xed\x81\xecn\xbd0", 'commit', 190)
('\xd2\x12\x83\xc7\x04E\x08\xcf:J\xac\xce\xfd\x91x\xf6u\x9c#\xda', 'tree', 34)

>>> for k in r.odb.sha_iter():
... r.odb.stream(k).read()
...
'hoge\n'
'hoge\nhoge\n'
'tree d21283c7044508cf3a4aaccefd9178f6759c23da\nauthor NISHIO Hirokazu 1303459640 +0900\ncommitter NISHIO Hirokazu 1303459640 +0900\n\nmsg\n'
'100644 README\x00"b\xde\x0c\x12\x1f"\xdf\x8ex\xf5\xa3}n\x11O\xd3"\xc0\xb0'



>>> r.branches # equals to r.heads?
[<git.Head "refs/heads/master">]
>>> r.refs
[<git.Head "refs/heads/master">]
>>> r.tags
[]

[1]+ Stopped python
f$ git branch foo
f$ cat >> README
hogehoge
f$ git commit -a
Waiting for Emacs...
[master 4c1458d] hogehoge
1 files changed, 1 insertions(+), 0 deletions(-)

f$ fg
python

>>> r.branches
[<git.Head "refs/heads/foo">, <git.Head "refs/heads/master">]
>>> [x.object for x in r.branches]
[<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">, <git.Commit "4c1458d2547c77d3166edd081b000f87cceb218e">]

[1]+ Stopped python
f$ git reset HEAD^
Unstaged changes after reset:
M README
f$ fg
python

>>> [x.object for x in r.branches]
[<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">, <git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">]

>>> r.head.object
<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">
>>>

2010-08-04

Thing you should know before moving your Django app into Google App Engine

Django's model has "id" field. It is auto-increment, unique integer for each models. GAE Datastore also has "id" field. It is *not* auto-increment, unique integer for *all* models. And you can not set the field as you want. This is annoying.

If you use Django's "id" field in permalink etc. (as I did), you need to add some field like "oldid", move old "id" value in it and write some code to put such a value in new coming items. For your information, when you can't inherit User models, you need to make a wrapper model. And also, you should change all code searching items with "id". You should change all "get_absolute_url" methods to use "oldid" in place of "id".

That is not all problem you should overcome. GAE Datastore doesn't allow inequality filter on more than one fields. If you use it, you should change the logic. Some of them are solvable with result caching. If you use SQL directly for speed-up, it won't work. Some query take more time or doesn't return correct value and there is DeadlineExceededException. You need tune-up in GAE way. You can't know how much items in your Datastore if it is more than 1000, in such case count() always return 1000.

Three month before, I thought it is easy task. Now I am convinced that I should rewrite the app. I hope it helps some people like me.

2010-07-13

Python Lost $20 Quiz

You can get $999 on the first day, and get a half of it (floored, that is $499) on the second day, and get half ... eventually how much you get in total? You received an e-mail, the payment in each days are printed in it, comma-separated. You split it with comma, eval and sum. Your answer is $1970. Oh, really is it correct?



>>> def format(xs):
return ",".join("%03d" % x for x in xs)

>>> format([123, 456, 789])
'123,456,789'



>>> def list_gen(n):
while n:
yield n
n /= 2

>>> list(list_gen(64))
[64, 32, 16, 8, 4, 2, 1]



>>> email = format(list_gen(999))
>>> sum(eval(s) for s in email.split(","))
1970



>>> total = 0
>>> 999
999
>>> total += _
>>> _ / 2
499
>>> total += _
>>> _ / 2
249
>>> total += _
>>> _ / 2
124
>>> total += _
>>> _ / 2
62
>>> total += _
>>> _ / 2
31
>>> total += _
>>> _ / 2
15
>>> total += _
>>> _ / 2
7
>>> total += _
>>> _ / 2
3
>>> total += _
>>> _ / 2
1
>>> total += _
>>> _ / 2
0
>>> total
1990

2010-05-27

FIX: ImportError: cannot import name ImmutableList

Hi, it is NISHIO Hirokazu. I'm moving my Django app into Google App Engine with Django-nonrel. I encountered the error and it took a lot of time from me. Google search didn't know anything about the problem.

The version of Django in Google App Engine is 1.0. Thus that of Django-nonrel(djangoappengine) is 1.2. It is the root of the problem.

Of cource, some code to avoid such a versioning problem was wrote in Django-nonrel. See djangoappengine/main/main.py


parent_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
f parent_dir not in sys.path:
sys.path.insert(0, parent_dir)

# Remove the standard version of Django
if 'django' in sys.modules and sys.modules['django'].VERSION < (1, 2):
for k in [k for k in sys.modules
if k.startswith('django\.') or k == 'django']:
del sys.modules[k]


If sys.path doesn't have the path to Django in Django-nonrel, it add the path. If already old Django was imported, it remove that. But unfortunately, in my case, sys.path has the path, and the path to Django in Google App Engine preceded, but it's not imported yet.

It's mystery. I can't tell why it happened. But it actually happened.

I wrote a workaround for the problem. It bring the path to correct Django always on top.


parent_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
if parent_dir in sys.path:
sys.path.remove(parent_dir)
sys.path.insert(0, parent_dir)

FIX: DatabaseError: Non-nullable field id can't be None!

Hi, it is NISHIO Hirokazu. I'm moving my Django app into Google App Engine with Django-nonrel. I encountered the error and it took a lot of time from me. Google search didn't know anything about the problem.

Keep in mind: DON'T INHERIT DJANGO MODELS!

I want to add "oldid" field to keep relation information between models. Unfortunately "User" models are not mine, just be imported from django.auth, so I inherit it to add the field. It was the root of evil.

I hope it helps you :)

2010-04-16

How I fixed TemplateNotFound error

Hi, it is NISHIO Hirokazu! Yesterday I deployed my brand-new application with kay-framework, but it doesn't work. It said TemplateNotFound occured. It worked on my local server.

It came from very simple mistake! I used Deploy button on my GoogleAppEngineLauncher. It run "appcfg.py update" directly. However, we should not use "appcfg.py update" directly. Use "manage.py appcfg update".

Jinja2 (used in Kay) pre-parse templates and compile them to python codes. You can do it to run "manage.py preparse_apps". And when you do "manage.py appcfg update", it also do preparsing. Unfortunately I did "appcfg.py update" so the lack of preparsing phase caused TemplateNotFound.

2009-10-16

Re: Python vs Clojure

This entry was moved to NISHIO Hirokazu's blog

2009-06-26

Python Spa #6

Hi, it is NISHIO Hirokazu, today I'm participating Python Onsen(Python Spa) #6. It is a hackathon of python hackers in a inn with hot spa. Pyshon Spa donates reminder of bills. You can see it on donation page of Python Software Foundation. You may remember the post about java-ja spa: Hacker's Cafe Blog: Hackathon with Spa. It is the same inn.


46 bananas!

A lot of pepsi!!

Nice Japanese dinner! Healthy foods!

2009-06-16

2009-03-16

Ruby's Struct v.s. Python's namedtuple

This entry was moved to NISHIO Hirokazu's blog