472,989 Members | 3,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

How would I write this C code in Python?

I am trying to figure out how to rewrite the following chunk of code
in Python:

C source
Expand|Select|Wrap|Line Numbers
  1. typedef struct PF
  2. {
  3. int flags;
  4. long user;
  5. char*filename;
  6. unsigned char buffer[MAXBUFFERSIZE];
  7. } PF;
  8.  
  9. typedef BLOCK
  10. {
  11. PF * packdata;
  12. } BLOCK;
  13.  
  14. BLOCK* blocks;
  15.  
My first idea was to create a class for PF and a class for BLOCK, but
I got lost somewhere along the lines. :\

Python Attempt: Please note that since I can't type TABs online
easily, I am using the @ character to represent TABs in the following
Python code.
Expand|Select|Wrap|Line Numbers
  1. class PF:
  2. @def __init__(self):
  3. @@self.flags, self.user = 0, 0
  4. @@self.filename = ''
  5. @@self.buffer = []
  6.  
  7. class BLOCK:
  8. @def __init__(self):
  9. @@self.packdata = []
  10.  
  11. blocks = []
  12.  
Any Python Gurus out there that can help me?

Jul 6 '07 #1
4 1383
On Fri, 06 Jul 2007 17:31:50 +0000, DeveloperX wrote:
I am trying to figure out how to rewrite the following chunk of code
in Python:

C source
Expand|Select|Wrap|Line Numbers
  1. typedef struct PF
  2. {
  3.   int flags;
  4.   long user;
  5.   char*filename;
  6.   unsigned char buffer[MAXBUFFERSIZE];
  7. } PF;
  8. typedef BLOCK
  9. {
  10.   PF * packdata;
  11. } BLOCK;
  12. BLOCK* blocks;
  13.  

My first idea was to create a class for PF and a class for BLOCK, but
I got lost somewhere along the lines. :\

Python Attempt: Please note that since I can't type TABs online
easily, I am using the @ character to represent TABs in the following
Python code.
Expand|Select|Wrap|Line Numbers
  1. class PF:
  2. @def __init__(self):
  3. @@self.flags, self.user = 0, 0
  4. @@self.filename = ''
  5. @@self.buffer = []
  6. class BLOCK:
  7. @def __init__(self):
  8. @@self.packdata = []
  9. blocks = []
  10.  

Any Python Gurus out there that can help me?
At a first glance it looks okay but unless we know what you are going to
do with these data structures it's hard to tell if it is really the best
"translation".

`PF.buffer` might be better a string or an `array.array`. And is `BLOCK`
really just a structure with *one* member? Looks a bit odd IMHO.

Ciao,
Marc 'BlackJack' Rintsch
Jul 6 '07 #2
On Fri, 06 Jul 2007 17:31:50 +0000, DeveloperX wrote:
Python Attempt: Please note that since I can't type TABs online
easily, I am using the @ character to represent TABs in the following
Python code.
Why not indent with spaces, just like you did for the example C code?
--
Steven.
Jul 7 '07 #3
Marc 'BlackJack' Rintsch a écrit :
On Fri, 06 Jul 2007 17:31:50 +0000, DeveloperX wrote:
>I am trying to figure out how to rewrite the following chunk of code
in Python:

C source
Expand|Select|Wrap|Line Numbers
  1. typedef struct PF
  2. {
  3.   int flags;
  4.   long user;
  5.   char*filename;
  6.   unsigned char buffer[MAXBUFFERSIZE];
  7. } PF;
  8. typedef BLOCK
  9. {
  10.   PF * packdata;
  11. } BLOCK;
  12. BLOCK* blocks;

My first idea was to create a class for PF and a class for BLOCK, but
I got lost somewhere along the lines. :\

Python Attempt: Please note that since I can't type TABs online
easily, I am using the @ character to represent TABs in the following
Python code.
Expand|Select|Wrap|Line Numbers
  1. class PF:
  2. @def __init__(self):
  3. @@self.flags, self.user = 0, 0
  4. @@self.filename = ''
  5. @@self.buffer = []
  6. class BLOCK:
  7. @def __init__(self):
  8. @@self.packdata = []
  9. blocks = []

Any Python Gurus out there that can help me?

At a first glance it looks okay but unless we know what you are going to
do with these data structures it's hard to tell if it is really the best
"translation".

`PF.buffer` might be better a string
Mmm... We can't tell for sure since we have no use case, but I'm not
sure that the OP wants an immutable object here.
or an `array.array`.
or a StringIO.
And is `BLOCK`
really just a structure with *one* member? Looks a bit odd IMHO.
+1

<OP>
Unless you have behaviours attached to this type, you may as well just
use lists.
</OP>

And while we're at it, Python's conventions are that ALL_UPPER names
denotes (pseudo) constants. For types, the conventions is to use
CantRememberIfItsCamelOrMixedCaseButYouShouldGetTh eIdeaByNow.

HTH
Jul 10 '07 #4
On Jul 10, 5:05 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
Mmm... We can't tell for sure since we have no use case, but I'm not
sure that the OP wants an immutable object here.
or an `array.array`.

or a StringIO.
And is `BLOCK`
really just a structure with *one* member? Looks a bit odd IMHO.

+1

<OP>
Unless you have behaviours attached to this type, you may as well just
use lists.
</OP>

And while we're at it, Python's conventions are that ALL_UPPER names
denotes (pseudo) constants. For types, the conventions is to use
CantRememberIfItsCamelOrMixedCaseButYouShouldGetTh eIdeaByNow.

HTH
I just used the same convention that the C code used in this post.
I write my python classes and functions in CamelCaps.

Not completely certain what to do at this point.

Jul 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MiLF | last post by:
Is it possible to write a Audio CD Player by using python only?
5
by: John Ladasky | last post by:
Hi, folks, At the beginning of 2003, I was a frustrated computer user, and lapsed programmer, with problems to solve that screamed for programming. Thanks to the Python language and community, I...
5
by: AMD | last post by:
Hi, I need to write a Win32 DLL and I would like to use Python instead of VB, C++ or Delphi. Is this possible? Thank you, Andre M. Descombes
33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
19
by: Levi Campbell | last post by:
Hi, I'm thinking about writing a system for DJing in python, but I'm not sure if Python is fast enough to handle the realtime audio needed for DJing, could a guru shed some light on this subject...
21
by: Raj | last post by:
Hi, We just executed a project with Python using TG. The feedback was to use more python like programming rather than C style code executed in Python. The feedback is from a Python purist and...
3
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
3
by: madsornomads | last post by:
Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on...
7
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.