471,310 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 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 1334
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John Ladasky | last post: by
5 posts views Thread by AMD | last post: by
33 posts views Thread by Nick Evans | last post: by
19 posts views Thread by Levi Campbell | last post: by
21 posts views Thread by Raj | last post: by
3 posts views Thread by Matthew Warren | last post: by
3 posts views Thread by madsornomads | last post: by
reply views Thread by rosydwin | last post: by

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.