Re: Python editor

2006-08-28 Thread Jerry Fleming
Larry Bates wrote:
> Jason Jiang wrote:
>> Hi,
>>
>> Could someone recommend a good Python editor? Thanks.
>>
>> Jason 
>>
>>
>>
> For just getting started use Idle that comes with Python.
> If you are already a user or if you are looking for a more
> powerful solution you can use Eclipse (with Python plug-in).
> These represent both ends of the spectrum in editors.
> 
> -Larry Bates

Vim (vim-python) or emacs (python.el) is always the best solution.
-- 
http://mail.python.org/mailman/listinfo/python-list


python game with curses

2006-04-28 Thread Jerry Fleming
Hi,

I have wrote a game with python curses. The problem is that I want to 
confirm before quitting, while my implementation doesn't seem to work. 
Anyone can help me?
[code]
#!/usr/bin/python
#
# Brick & Ball in Python
# by Jerry Fleming <[EMAIL PROTECTED]>
#
# This is a small game adapted from that in Motorola Mobile C289, and my 
first game in python :)
#
# This progrma is best run under linux. Since Windows port of Python has 
poor curses support,
# play it under Windows is not recommended. If you have no linux box 
available, try Cygwin,
# though it too has poor curses support.
#
# As I am a newbie to python, please tell me if you have a better 
implementation or any suggestions.
#
# TODO:
# re-implemente it with wxPython, so one does not have to rely on the 
ugly curses.
# session support.
# pausing, especially when confirming
# resize terminal at run time
#
# HISTORY
# 2006-04-19: first version
#
#

import curses
import _curses
import thread
from time import sleep
from string import split
from random import randint

# parameters: adjust them to fit you terminal
brick_width = 7
brick_gap_x = 1
brick_gap_y = 1
speed = 0.05 # sleep time to control moving speed of the ball
pause = 1 # time to pause

# terminal initialization
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(1)
screen_height, screen_width = stdscr.getmaxyx()
screen_height = screen_height - 1
screen_width = screen_width - 1
brick_rows = screen_height / 4
if brick_rows > 7: brick_rows = 7
brick_cols = (screen_width + brick_gap_x) / (brick_width + brick_gap_x)
brick_margin = (screen_width - brick_cols * (brick_width + brick_gap_x) 
+ brick_gap_x)/2
pad_position = randint(0, screen_width - brick_width)
ball_position = [screen_height - 3, randint(0, screen_width - brick_width)]
ball_direction = [-1, 1] # abs(tan(a)) must be 1
char = ''
bricks = []
game_score = 0
ScreenSizeError = 'ScreenSizeError'

tStart = '''
  ___   _ ___  ___ _ _ 
 __ _
(  \  (_) | |   / _ \(  \  | | |   (_) 
   (_ \_  | |
  )  )  _  | |  _   ( (_) ))  )_| | |_  
 _) )   _ _| |_| |__   ___  
|  __  ( / ___) |/ ___) |_/ )   ) _ (|  __  (( | | |   | |  _ \ 
   |  / | | (_   _)  _ \ / _ \|  _ \
| |__)  ) |   | ( (___|  _ (   ( (/  \   | |__)  ) ___ | | |   | | | | | 
  | || |_| | | |_| | | | |_| | | | |
|__/|_|   |_|\)_| \_)   \__/\_)  |__/\_|\_)_)  |_|_| |_| 
  |_| \__  |  \__)_| |_|\___/|_| |_|
 
       (____/


by Jerry Fleming <[EMAIL PROTECTED]>


   GAME STARTING ...

(this assumes that your terminal be larger that 
130x40)
'''

tExit = '''
8 
   8
888 88   ad8ba 88 8bd8  d8 
 88
88  ""   ,d d8" "8b88  Y8,,8P ,8P' 
 88
88   88 ""  a8P88   Y8,  ,8P d8" 
 88
88a 8b, ,d8 88 MM88MMM   ,a8P" 88"8aa8",8P' 
8b,dPPYba,  88
88"""""  `Y8, ,8P'  88   88 d8"88 `88'd8"   88P' 
   `"8a 88
88 )888(88   88 "" 88  88   ,8P'88 
  88 88
88   ,d8" "8b,  88   88,aa 88  88  d8"  88 
  88 88
888 8P' `Y8 88   "Y888  88 88  88 8P'   88 
  88 88
8 
   8
'''

tOver = '''
   ,adba, 
  88
  d8"'`"8b 
  88
d8' 
 88
88,adPPYYba, 88,dPYba,,adPYba,   ,adPPYba, ,adPPYba,  8b 
   d8  ,adPPYba, 8b,dPPYba, 88
88  8 "" `Y8 88P'   "88""8a a8P_88a8" "8a 
`8b d8' a8P_88 88P'   "Y8 88
Y8,88 ,adP88 88  88  88 8PP"""""""8b   d8 
`8b   d8'  8PP""""""" 88 ""
  Y8a..a88 88,,88 88  88  88 "8b,   ,aa"8a,   ,a8" 
  `8b,d8'   "8b,   ,aa 88 aa
   `"Y8P"  `"8bbdP"Y8 88  88  88  `"Ybbd8"' `"YbbdP"' 
"8"  `"Ybbd8"' 88 88
'''

tGoon = '''
   

ctypes in python failed to honor c_int

2008-03-17 Thread Jerry Fleming
Hi,

I have a binary file written with c structures. Each record contains a 
null-terminated string followed by two 4-bytes integers. I wrote a small 
segment of python code to parse this file in this way:
[coe]
#!/usr/bin/python

from ctypes import *

class Entry(Structure):
_fields_ = ('w', c_char_p), ('s', c_uint, 32), ('l', c_uint, 32)

idx = open('x.idx', 'rb')
str = idx.read(1000)
obj = Entry(str)
print obj.w
print obj.s
print obj.l
[/code]
where the field w is the string, and s and l are the integers. Problem 
is that, I can only get the strings, not the integers. Well, I did got 
integers, but they are all zeros. What should I do to get the real numbers?

Thanks for help.

- Jerry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes in python failed to honor c_int

2008-03-17 Thread Jerry Fleming
Gabriel Genellina wrote:
> On 17 mar, 23:57, Jerry Fleming <[EMAIL PROTECTED]> wrote:
> 
>> I have a binary file written with c structures. Each record contains a
>> null-terminated string followed by two 4-bytes integers. I wrote a small
>> segment of python code to parse this file in this way:
>> [coe]
>> #!/usr/bin/python
>>
>> from ctypes import *
>>
>> class Entry(Structure):
>> _fields_ = ('w', c_char_p), ('s', c_uint, 32), ('l', c_uint, 32)
>>
>> idx = open('x.idx', 'rb')
>> str = idx.read(1000)
>> obj = Entry(str)
>> print obj.w
>> print obj.s
>> print obj.l
>> [/code]
>> where the field w is the string, and s and l are the integers. Problem
>> is that, I can only get the strings, not the integers. Well, I did got
>> integers, but they are all zeros. What should I do to get the real numbers?
> 
> So the string has a variable length? For "Hello" you have
> 'h','e','l','l','o', a zero byte, followed by the two integers? This
> is somewhat unusual for a C struct (in fact you can't declare it in
> C). Perhaps the string is actually a char[n] array with a declared
> maximum size?

Yes, it has a variable length. The C version of the structure is 
something like this:
[code]
struct entry {
 char *str,
 int start,
 int length
}
[/code]
And adding repr() would print something like this:
[code]
'(as) mad as a 
[EMAIL PROTECTED];\x00\x00\x00`.GIF\x00\x00\x00'
(as) mad as a hatter
0
0
[/code]
where the first line is the result of repr(). We can find that, after 
the null-terminated string '(as) mad as a hatter', there are two 
integers, 0 and 31 (0x1f). But python treat 31 as zero.

> 
> Try printing repr(a_few_read_bytes) or a_few_read_bytes.encode("hex")
> to see the actual file contents.
> 
> --
> Gabriel Genellina
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes in python failed to honor c_int

2008-03-18 Thread Jerry Fleming
Gabriel Genellina wrote:
> On 18 mar, 04:12, Jerry Fleming <[EMAIL PROTECTED]> wrote:
>> Gabriel Genellina wrote:
>>> On 17 mar, 23:57, Jerry Fleming <[EMAIL PROTECTED]> wrote:
>>>> I have a binary file written with c structures. Each record contains a
>>>> null-terminated string followed by two 4-bytes integers. I wrote a small
>>>> segment of python code to parse this file in this way:
>>>> [coe]
>>>> #!/usr/bin/python
>>>> from ctypes import *
>>>> class Entry(Structure):
>>>> _fields_ = ('w', c_char_p), ('s', c_uint, 32), ('l', c_uint, 32)
>>>> idx = open('x.idx', 'rb')
>>>> str = idx.read(1000)
>>>> obj = Entry(str)
>>>> print obj.w
>>>> print obj.s
>>>> print obj.l
>>>> [/code]
>>>> where the field w is the string, and s and l are the integers. Problem
>>>> is that, I can only get the strings, not the integers. Well, I did got
>>>> integers, but they are all zeros. What should I do to get the real numbers?
>>> So the string has a variable length? For "Hello" you have
>>> 'h','e','l','l','o', a zero byte, followed by the two integers? This
>>> is somewhat unusual for a C struct (in fact you can't declare it in
>>> C). Perhaps the string is actually a char[n] array with a declared
>>> maximum size?
>> Yes, it has a variable length. The C version of the structure is
>> something like this:
>> [code]
>> struct entry {
>>  char *str,
>>  int start,
>>  int length}
>>
>> [/code]
> 
> But this doesn't match the file contents. There are no pointers in the
> file.
> 
>> And adding repr() would print something like this:
>> [code]
>> '(as) mad as a
>> [EMAIL PROTECTED];\x00\x00\x00`.GIF\­x00\x00\x00'
>> (as) mad as a hatter
>> 0
>> 0
>> [/code]
>> where the first line is the result of repr(). We can find that, after
>> the null-terminated string '(as) mad as a hatter', there are two
>> integers, 0 and 31 (0x1f). But python treat 31 as zero.
> 
> Ah, but it doesn't "treat 31 as zero". Entry(str) is the same as
> Entry(w=str), that is, you are initializing the w attribute alone,
> leaving the other two integers as 0.
> I don't know how to use ctypes to read the structure (nor if it is
> possible at all), I would read it normally with Python code and build
> the struct afterwards (in case it is used to call any C code).
> 
> w, data = data.split('\x00', 1)
> s, l = struct.unpack("ll", data[:8])
> data= data[8:]
> 
> --
> Gabriel Genellina
Oh yes. What an idiot I am. Thanks Gabriel very much.
-- 
http://mail.python.org/mailman/listinfo/python-list


do 'os.path' include 'os' for us?

2008-03-27 Thread Jerry Fleming
Hi,

I wrote a python script to list files in a directory but somehow did it 
wrongly by importing os.path instead of os. To my astonishment, it works 
just as charm:
#!/usr/bin/python
import os.path
for file in os.listdir('/root/'):
print file

I was wondering why? os.path doesn't contain listdir, why there is no 
complaint like 'os: unknown name'? Does this mean, instead of importing 
os, we can import os.path?

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 有中国人乎?

2008-04-14 Thread Jerry Fleming
Penny Y. wrote:
> [EMAIL PROTECTED] 写道:
>> Python这种语言有前途吗?在下想学他一学.
> 
> hehe, so humorous you are!
> Yes I think python has good future.
> But it depends on what you use it to do.
> If you're a singer, a financier, a historian etc, you don't need python.
A singer uses his/here throat; a financier uses the abacus -- well, 
traditionally. Anyway, python is useful only when computer is useful. It 
is helpful for programming guru, and newbies as well to do the 'quick 
tricks'.
> But if you are playing in computer programming, it's valuable for you to 
> take some time learning python.
> btw,I'm also newbie to python,but I like it.
> 
> --penny
-- 
http://mail.python.org/mailman/listinfo/python-list

sqlalchemy: how to define association object with declarative style?

2010-09-07 Thread Jerry Fleming
Hi,

I want to define the relationship for my users and their groups with
declarative style (so that the relating model can inherit Timestamp
mixin and Tablename mixin):

class User(DeclarativeBase, Tablename, TimestampMixin):
'''User avatar is named after its id.
A user may be a student or teacher or both.
'''
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
username = Column(String(30), index=True, nullable=False, unique=True)
password = Column(String(30), index=True, nullable=False)

class Group(DeclarativeBase, Tablename, TimestampMixin):
id = Column(Integer, Sequence('group_id_seq'), primary_key=True)
name = Column(Unicode(20), unique=True, nullable=False)
display = Column(Unicode(255))

class GroupUser(DeclarativeBase, Tablename, TimestampMixin):
id = Column(Integer, Sequence('group_user_id_seq'), primary_key=True)
user = Column(Integer, ForeignKey('user.id'), index=True),
group = Column(Integer, ForeignKey('group.id'), index=True)

I was wondering how to associate User and Group with GroupUser with
GroupUser as the association object/proxy? The sqlalchemy docs only
mention only mention association tables, or non-declarative manual mapping.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


re and locale/unicode

2010-09-20 Thread Jerry Fleming
Hi,

Having the following python code:


import locale
import re

locale.setlocale(locale.LC_ALL, 'zh_CN.utf8')
re.findall('(?uL)\s+', u'\u2001\u3000\x20', re.U|re.L)
re.findall('\s+', u'\u2001\u3000\x20', re.U|re.L)
re.findall('(?uL)\s+', u'\u2001\u3000\x20')


I was wondering why doesn't it find the unicode space chars \u2001 and
\u3000? The python docs for re module says:

When the LOCALE and UNICODE flags are not specified, matches any
whitespace character; this is equivalent to the set [ \t\n\r\f\v]. With
LOCALE, it will match this set plus whatever characters are defined as
space for the current locale. If UNICODE is set, this will match the
characters [ \t\n\r\f\v] plus whatever is classified as space in the
Unicode character properties database.

which doesn't seem to work. Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list