How would I write this C code in Python?

D

DeveloperX

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

C source
Code:
typedef struct PF
{
  int flags;
  long user;
  char*filename;
  unsigned char buffer[MAXBUFFERSIZE];
} PF;

typedef BLOCK
{
  PF * packdata;
} BLOCK;

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.
Code:
class PF:
@def __init__(self):
@@self.flags, self.user = 0, 0
@@self.filename = ''
@@self.buffer = []

class BLOCK:
@def __init__(self):
@@self.packdata = []

blocks = []

Any Python Gurus out there that can help me?
 
M

Marc 'BlackJack' Rintsch

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

C source
Code:
typedef struct PF
{
int flags;
long user;
char*filename;
unsigned char buffer[MAXBUFFERSIZE];
} PF;

typedef BLOCK
{
PF * packdata;
} BLOCK;

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.
Code:
class PF:
@def __init__(self):
@@self.flags, self.user = 0, 0
@@self.filename = ''
@@self.buffer = []

class BLOCK:
@def __init__(self):
@@self.packdata = []

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 or an `array.array`. And is `BLOCK`
really just a structure with *one* member? Looks a bit odd IMHO.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steven D'Aprano

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?
 
B

Bruno Desthuilliers

Marc 'BlackJack' Rintsch a écrit :
I am trying to figure out how to rewrite the following chunk of code
in Python:

C source
Code:
typedef struct PF
{
int flags;
long user;
char*filename;
unsigned char buffer[MAXBUFFERSIZE];
} PF;

typedef BLOCK
{
PF * packdata;
} BLOCK;

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.
Code:
class PF:
@def __init__(self):
@@self.flags, self.user = 0, 0
@@self.filename = ''
@@self.buffer = []

class BLOCK:
@def __init__(self):
@@self.packdata = []

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
CantRememberIfItsCamelOrMixedCaseButYouShouldGetTheIdeaByNow.

HTH
 
D

DeveloperX

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 a StringIO.


+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
CantRememberIfItsCamelOrMixedCaseButYouShouldGetTheIdeaByNow.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top