Re: [Tutor] Tutor Digest, Vol 106, Issue 42

2012-12-17 Thread Peter Otten
Marefe Serentas wrote:

> On 12/18/2012 12:38 AM, tutor-requ...@python.org wrote:
>> Re: Get the structure values from a c file
> I apologize for some syntax errors in my c file. I would like to correct
> my mistakes and append some lines in the c file to make it clearer.
> 
> 
> 
> #define max (3)
> #define max_data_size (9600*2*8)
> 
> typedef unsigned char  u8;
> typedef signed int u32;
> 
> int some_data[] =
> {
> -288,   -153, 31, 24,205,110, 39, 88,
> -281,145, 35,266, 63,-79,   -103,-25,
> 53,145,   -114,   -274, 46, 60,220,205
> };
> 
> typedef struct A
> {
>  u32 a;
>  u8 b;
> }A;
> 
> typedef struct MainStruct
> {
>  A var1;
>  u32 var2;
>  u32 var3[max];
>  u32 var4;
>  u32 var5[max];
> }MainStruct;
> 
> void generate()
> {
>  MainStruct myMain = {0};
>  myMain.var1.a = 1;
>  myMain.var1.b = 'a';
>  myMain.var2 = 3;
>  myMain.var3[0] = -3;
>  myMain.var3[1] = 6;
>  myMain.var3[2] = 18;
> 
>  (void)memcpy((void *)myMain.var4,
>   (void *)some_data,
>   sizeof(some_data));
> 
>  myMain.var5[0] = 1;
> }
> 
> 
> 
> 
> This is a work-problem. Given a c file as input, the customer wants me
> to write a python script that will retrieve the values of myMain.
> He wants those values converted to binary data and write it in a .bin
> file. As a result, the size of the bin file is equal to the size of
> myMain. The c file input content might change in the future like
> different values assigned, added fields in the MainStruct, etc. Also I
> am not permitted to change the c file.
> 
> 
> About me, I'm a fresh graduate. I just started learning python a month
> ago. I learned c in school, we had it for 2 years.
> 
> 
> I'm using python 2.6. I'm running on Windows 7 64-bit OS.
> 
> What I did so far is parse the C source code.
> But having a problem parsing the value of myMain.var4.

How about running the C source with an added dump() function

dump(MainStruct *s, char *filename)
{
  FILE * f = fopen(filename, "wb");
  fwrite(f, sizeof(MainStruct), 1, f);
  fclose(f);
}

in the right places?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Image Processing

2012-12-17 Thread Ashkan Rahmani
Hello Eike,
Thank you for reply...

On Tue, Dec 18, 2012 at 1:09 AM, Eike Welk  wrote:
> Hello Ashkan!
>
>
> On Saturday 15.12.2012 23:20:31 Ashkan Rahmani wrote:
>> I have developed some simple c++/qt/opencv application with face
>> detection functionality.
>> for some reasons I'm going to start again them and I wan to use python 3.
>> Unfortunately I found opencv not supported in python 3.
>> 1- as I'm new in python programming, is python 3 good choice for me?
>
> Python 2 and Python 3 are very similar. Use the latest version for which all
> libraries are available, possibly Python 2.7. The biggest difference between
> the languages for a beginner is:
>
> Python 2:
>   print "Hello World"
>
> Python 3:
>   print("Hello World")
>
> Furthermore Python is a very simple language. As you can already program in
> C++, it will take you two afternoons to learn Python. Use the tutorial from
> the official documentation:
>
> http://www.python.org/doc/
>
since I asked these questions in this mailing list, I decided to start
with 2.7, beside it, I installed 3.3 from source to port my learning
to latest version.
http://www.python.org/doc/
Yes, the best start point is this link!
>
>> 2- Opencv binding will be available for python 3?
>
> Ask the makers of Opencv. But I expect that they eventually will switch to
> Python 3, because Python 2 is not further developed.
>
> However not all Linux distributions have currently moved to Python 3: OpenSuse
> comes with Python 2.7 while Ubuntu has already moved to Python 3.
>
may be the best library/tools for industrial level image processing is
OpenCV, Nothing an compare to it.
>
>> 3- Is there any other image processing library for python 3?
>
> * Numpy is an N-dimensional array library. It is much more low level than
> Opencv, but can be used to prototype algorithms, that you later implement in
> C, C++, or Cython. Opencv images can directly be converted to Numpy arrays and
> vice versa.
>
> http://www.numpy.org/
> http://www.cython.org/
>
> * Scipy (which needs Numpy) contains some image analysis algorithms, but is
> still more low level than most of Opencv.
>
> http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html
>
as you said, these are really low level. If I have to stick to python
maybe I use theme.

> * PIL is not really suitable for what you want. It is intended for loading and
> saving images, converting image formats, adjusting colors, and similar tasks.
> PIL images can also be directly converted to Opencv and Numpy images.
>
> http://www.pythonware.com/library/pil/handbook/index.htm
>
you are righ, it seems PIL is good for prepare input data befor processing.
>
>> 4- Basically python 3 or 2.7 is suitable for image processing?
>
> I think you should use Python 2.7 because Opencv is currently not ported to
> Python 3.
>
I goggled,  Beside python ruby have some good features, I don't know what to do,
Now I want to learn python, it's really great for develop.
>
> Eike.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Best Regards,
Ashkan R < ashkan...@gmail.com >
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get the structure values from a c file

2012-12-17 Thread Marefe Serentas

On 12/18/2012 12:38 AM, tutor-requ...@python.org wrote:

Re: Get the structure values from a c file

I apologize for some syntax errors in my c file. I would like to correct
my mistakes and append some lines in the c file to make it clearer.



#define max (3)
#define max_data_size (9600*2*8)

typedef unsigned char  u8;
typedef signed int u32;

int some_data[] =
{
-288,   -153, 31, 24,205,110, 39, 88,
-281,145, 35,266, 63,-79,   -103,-25,
  53,145,   -114,   -274, 46, 60,220,205
};

typedef struct A
{
u32 a;
u8 b;
}A;

typedef struct MainStruct
{
A var1;
u32 var2;
u32 var3[max];
u32 var4;
u32 var5[max];
}MainStruct;

void generate()
{
MainStruct myMain = {0};
myMain.var1.a = 1;
myMain.var1.b = 'a';
myMain.var2 = 3;
myMain.var3[0] = -3;
myMain.var3[1] = 6;
myMain.var3[2] = 18;

(void)memcpy((void *)myMain.var4,
 (void *)some_data,
 sizeof(some_data));

myMain.var5[0] = 1;
}




This is a work-problem. Given a c file as input, the customer wants me
to write a python script that will retrieve the values of myMain.
He wants those values converted to binary data and write it in a .bin
file. As a result, the size of the bin file is equal to the size of
myMain. The c file input content might change in the future like
different values assigned, added fields in the MainStruct, etc. Also I
am not permitted to change the c file.


About me, I'm a fresh graduate. I just started learning python a month
ago. I learned c in school, we had it for 2 years.


I'm using python 2.6. I'm running on Windows 7 64-bit OS.

What I did so far is parse the C source code.
But having a problem parsing the value of myMain.var4.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 106, Issue 42

2012-12-17 Thread Marefe Serentas

On 12/18/2012 12:38 AM, tutor-requ...@python.org wrote:

Re: Get the structure values from a c file
I apologize for some syntax errors in my c file. I would like to correct 
my mistakes and append some lines in the c file to make it clearer.




#define max (3)
#define max_data_size (9600*2*8)

typedef unsigned char  u8;
typedef signed int u32;

int some_data[] =
{
-288,   -153, 31, 24,205,110, 39, 88,
-281,145, 35,266, 63,-79,   -103,-25,
  53,145,   -114,   -274, 46, 60,220,205
};

typedef struct A
{
u32 a;
u8 b;
}A;

typedef struct MainStruct
{
A var1;
u32 var2;
u32 var3[max];
u32 var4;
u32 var5[max];
}MainStruct;

void generate()
{
MainStruct myMain = {0};
myMain.var1.a = 1;
myMain.var1.b = 'a';
myMain.var2 = 3;
myMain.var3[0] = -3;
myMain.var3[1] = 6;
myMain.var3[2] = 18;

(void)memcpy((void *)myMain.var4,
 (void *)some_data,
 sizeof(some_data));

myMain.var5[0] = 1;
}




This is a work-problem. Given a c file as input, the customer wants me 
to write a python script that will retrieve the values of myMain.
He wants those values converted to binary data and write it in a .bin 
file. As a result, the size of the bin file is equal to the size of 
myMain. The c file input content might change in the future like 
different values assigned, added fields in the MainStruct, etc. Also I 
am not permitted to change the c file.



About me, I'm a fresh graduate. I just started learning python a month 
ago. I learned c in school, we had it for 2 years.



I'm using python 2.6. I'm running on Windows 7 64-bit OS.

What I did so far is parse the C source code.
But having a problem parsing the value of myMain.var4.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread eryksun
On Mon, Dec 17, 2012 at 1:00 PM, Alan Gauld  wrote:
>
> Python uses its own C code for this.

The important point here is that they use the strtol/strtod interface,
however it's implemented. atoi and atof lack the end pointer argument
that enables raising a ValueError for an incomplete conversion. For
example, strtol("123e9", &end, 10) will merrily return 123, but with
*end == 'e'. I think it's good that Python raises a ValueError in this
case.

Errors should never pass silently.
Unless explicitly silenced.

http://www.python.org/dev/peps/pep-0020/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh connection

2012-12-17 Thread Prasad, Ramit
Please always include the list in your response (I have CC-ed the list).

Ufuk Eskici wrote:
> 
> Subprocess seems complicated, didnt understand.
> 
> Also tried pexpect but couldnt install on Windows.

Subprocess can be daunting to try and pick up, but it is
not as bad as it looks at first glance. Subprocess gives you 
a way to run something from the cmd prompt (the Windows shell) 
and send/retrieve data from the prompt. It may be complicated 
depending on what commands you actually want to run and the type 
of output you are trying to retrieve.

In order to make it work you need to be able to run the 
commands from cmd. Of course, you need to install an SSH 
client that will work from CMD prompt.

Here are some links that might help.
http://stackoverflow.com/questions/2407095/how-to-use-ssh-from-windows-cmd
http://community.spiceworks.com/topic/205271-ssh-from-windows-command-line


> 
> 2012/12/17 Prasad, Ramit 
> > Ufuk Eskici wrote:
> > >
> > > Hello All,
> > >
> > > Can we make an SSH connection with Pyhton 3.3 ?
> > >
> > > I want to connecto to my router, send commands and receive outputs.
> > >
> > > Thanks.
> > > Ufuk
> > You can try to use Paramiko. You may need to build it and it seems like
> > it may work except for SFTP.
> > 
> > https://github.com/paramiko/paramiko/issues/16
> > 
> > If you switch to Python 2.x you can use Fabric (which relies on Paramiko).


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Image Processing

2012-12-17 Thread Eike Welk
Hello Ashkan!


On Saturday 15.12.2012 23:20:31 Ashkan Rahmani wrote:
> I have developed some simple c++/qt/opencv application with face
> detection functionality.
> for some reasons I'm going to start again them and I wan to use python 3.
> Unfortunately I found opencv not supported in python 3.
> 1- as I'm new in python programming, is python 3 good choice for me?

Python 2 and Python 3 are very similar. Use the latest version for which all 
libraries are available, possibly Python 2.7. The biggest difference between 
the languages for a beginner is:

Python 2:
  print "Hello World"

Python 3:
  print("Hello World")

Furthermore Python is a very simple language. As you can already program in 
C++, it will take you two afternoons to learn Python. Use the tutorial from 
the official documentation:

http://www.python.org/doc/


> 2- Opencv binding will be available for python 3?

Ask the makers of Opencv. But I expect that they eventually will switch to 
Python 3, because Python 2 is not further developed. 

However not all Linux distributions have currently moved to Python 3: OpenSuse 
comes with Python 2.7 while Ubuntu has already moved to Python 3. 


> 3- Is there any other image processing library for python 3?

* Numpy is an N-dimensional array library. It is much more low level than 
Opencv, but can be used to prototype algorithms, that you later implement in 
C, C++, or Cython. Opencv images can directly be converted to Numpy arrays and 
vice versa.

http://www.numpy.org/
http://www.cython.org/

* Scipy (which needs Numpy) contains some image analysis algorithms, but is 
still more low level than most of Opencv.

http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html

* PIL is not really suitable for what you want. It is intended for loading and 
saving images, converting image formats, adjusting colors, and similar tasks. 
PIL images can also be directly converted to Opencv and Numpy images.

http://www.pythonware.com/library/pil/handbook/index.htm


> 4- Basically python 3 or 2.7 is suitable for image processing?

I think you should use Python 2.7 because Opencv is currently not ported to 
Python 3.


Eike.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh connection

2012-12-17 Thread Prasad, Ramit
Ufuk Eskici wrote:
> 
> Hello All,
> 
> Can we make an SSH connection with Pyhton 3.3 ?
> 
> I want to connecto to my router, send commands and receive outputs.
> 
> Thanks.
> Ufuk

You can try to use Paramiko. You may need to build it and it seems like 
it may work except for SFTP.

https://github.com/paramiko/paramiko/issues/16

If you switch to Python 2.x you can use Fabric (which relies on Paramiko).


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Alan Gauld

On 17/12/12 14:36, Oscar Benjamin wrote:


> Even stranger since the underlying atoi() C function



Also, are you sure that atoi() is used in CPython?


Nope, just making assumptions! :-0

As Eryksun points out Python uses its own C code for this.

assume == ass u me :-(

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh connection

2012-12-17 Thread Brad Hudson
Look at the subprocess module. You can send your SSH connections via
subprocess and collect the output for parsing.


On Mon, Dec 17, 2012 at 7:40 AM, Ufuk Eskici  wrote:

> Hello All,
>
> Can we make an SSH connection with Pyhton 3.3 ?
>
> I want to connecto to my router, send commands and receive outputs.
>
> Thanks.
> Ufuk
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread eryksun
On Mon, Dec 17, 2012 at 3:55 AM, Alan Gauld  wrote:
>
> So you are right, there is an inconsistency between how int() converts
> floating point numbers and how it converts strings. Even stranger since the
> underlying atoi() C function appears to handle float strings quite
> happily...

If you're using strtol in C it's up to you how to interpret an
incomplete conversion due to an out-of-range number or bad literal for
the given base. Python, on the other hand, automatically switches the
type for big integers to a multiprecision long (2.x long) and raises
ValueError for bad literals. Where you go from there is up to you.

BTW, 2.x int() isn't using the libc atoi or strtol/strtoul functions.
It has its own implementation in mystrtoul.c. PyOS_strtol wraps the
main workhorse PyOS_strtoul (unsigned):

http://hg.python.org/cpython/file/70274d53c1dd/Python/mystrtoul.c#l80

The conversion loop proceeds up to the first non-base character, as
defined by the table _PyLong_DigitValue in longobject.c:

http://hg.python.org/cpython/file/70274d53c1dd/Objects/longobject.c#l1604

A pointer to the last scanned character is set. In PyInt_FromString,
if this points to the first character, or the previous character isn't
alphanumeric, or removing trailing whitespace starting from this point
doesn't end on a NUL terminator (e.g. *end == '.'), then the
conversion raises a ValueError. See lines 364-383 in intobject.c:

http://hg.python.org/cpython/file/70274d53c1dd/Objects/intobject.c#l340
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Oscar Benjamin
On 17 December 2012 08:55, Alan Gauld  wrote:
>
> On 17/12/12 04:19, boB Stepp wrote:
>
>> It is apparent that int() does not like strings with floating-point
>> formats. None of my books (as far as my flipping can tell) or the
>> below built-in help clarify this:
>> ...
>>
>> Of course if I type int(float('10.0')) I get the desired 10 .
>
>
> as indeed will
>
> int(10.0)
>
> So you are right, there is an inconsistency between how int() converts 
> floating point numbers and how it converts strings. Even stranger since the 
> underlying atoi() C function appears to handle float strings quite happily...

The atoi() function like many of the older C functions has a major
flaw in that it indicates an error by returning zero even though zero
is actually a possible return value for the function. As far as I can
tell it doesn't even set an error code on failure. As a result it is
not safe to use without some additional checking either before or
after the call.

Python's int() function and C's atoi() function also accept and ignore
whitespace around the number in the string:

>>> int('   123   ')
123
>>> int('\t\n   \n   123  \n ')
123
>>> int('\t\n   \n   123  \n 456')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '123  \n 456'

In C, atoi() would have allowed that last example and given 123 as the result.

Also, are you sure that atoi() is used in CPython? The int() function
accepts an optional base argument and can process non-decimal strings:

>>> int('0xff', 16)
255
>>> int('0o377', 8)
255
>>> int('0b', 2)
255
>>> int('', 2)
255


>> So, I am guessing that to convert strings to integers with int() that
>> the string must already be of integer format? What is the rationale
>> for setting up int() in this manner?

I think it's unfortunate that Python's int() function combines two
distinct behaviours in this way. In different situations int() is used
to:
1) Coerce an object of some type other than int into an int without
changing the value of the integer that the object represents.
2) Round an object with a non-integer value to an integer value.

There are situations where behaviour 1) is required but behaviour 2)
is definitely not wanted. The inability to do this safely in Python
resulted in PEP 357 [1] that adds an __index__ method to objects that
represent integers but are not of type int(). Unfortunately, this was
intended for slicing and doesn't help when converting floats and
strings to int().

I have often found myself writing awkward functions to prevent a
rounding error from occurring when coercing an object with int().
Here's one:

def make_int(obj):
'''Coerce str, float and int to int without rounding error
Accepts strings like '4.0' but not '4.1'
'''
fnum = float('%s' % obj)
inum = int(fnum)
assert inum == fnum
return inum


References:
[1] http://www.python.org/dev/peps/pep-0357/


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh connection

2012-12-17 Thread Tom Tucker
One option is PyExpect

http://www.noah.org/python/pexpect/



On Mon, Dec 17, 2012 at 8:40 AM, Ufuk Eskici  wrote:

> Hello All,
>
> Can we make an SSH connection with Pyhton 3.3 ?
>
> I want to connecto to my router, send commands and receive outputs.
>
> Thanks.
> Ufuk
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ssh connection

2012-12-17 Thread Ufuk Eskici
Hello All,

Can we make an SSH connection with Pyhton 3.3 ?

I want to connecto to my router, send commands and receive outputs.

Thanks.
Ufuk
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get the structure values from a c file

2012-12-17 Thread eryksun
On Sun, Dec 16, 2012 at 10:12 PM, Marefe Serentas  wrote:
>
> I want to create a python script that will store the value of the structure
> MainStruct from the c file.


Try pycparser to parse C source code. To load a library and call its
functions you can use ctypes.

http://docs.python.org/2/library/ctypes
http://docs.python.org/3/library/ctypes
http://code.google.com/p/pycparser

Below I adapted your example for ctypes.


> #define max (3)
>
> typedef struct A
> {
> int a;
> char b;
> }A;
>
> typedef struct MainStruct
> {
> A var1;
> int var2;
> int var3[max];
> }Main;


import ctypes

max = 3

class A(ctypes.Structure):
_fields_ = [
  ('a', ctypes.c_int),
  ('b', ctypes.c_char),
]

class Main(ctypes.Structure):
_fields_ = [
  ('var1', A),
  ('var2', ctypes.c_int),
  ('var3', ctypes.c_int * max),
]


Per your typedef, the 2nd structure is named "Main".
c_int * max creates an array.


> void generate()
> {
> MainStruct myMain = {0};
> myMain.var1.a = 1
> myMain.var1.b = 'a'
> myMain.var2 = 3
> myMain.var3[0] = -3
> myMain.var3[1] = 6
> myMain.var3[2] = 18
> }


That would have to be "struct MainStruct myMain". Also, you're missing
semicolons on all but the first line.

It's simpler to let the caller manage memory. Here's the function
written to take a pointer:


void generate(Main *myMain)
{
myMain->var1.a = 1;
myMain->var1.b = 'a';
myMain->var2 = 3;
myMain->var3[0] = -3;
myMain->var3[1] = 6;
myMain->var3[2] = 18;
}


To call this, first load the library (e.g. "lib.so") and define the
prototype in Python. Setting "argtypes" is helpful because ctypes type
checks arguments. A TypeError is better than a segfault.


lib = ctypes.cdll.LoadLibrary('./lib.so')
generate = lib.generate
generate.argtypes = [ctypes.POINTER(Main)]
generate.restype = None


>>> myMain = Main()
>>> generate(myMain)
>>> myMain.var1.a, myMain.var1.b, myMain.var2
(1, 'a', 3)
>>> list(myMain.var3)
[-3, 6, 18]

It passes a pointer to myMain based on the argtypes prototype, but you
can also explicitly use generate(ctypes.byref(myMain)).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-17 Thread Alan Gauld

On 17/12/12 04:19, boB Stepp wrote:


It is apparent that int() does not like strings with floating-point
formats. None of my books (as far as my flipping can tell) or the
below built-in help clarify this:
...
Of course if I type int(float('10.0')) I get the desired 10 .


as indeed will

int(10.0)

So you are right, there is an inconsistency between how int() converts 
floating point numbers and how it converts strings. Even stranger since 
the underlying atoi() C function appears to handle float strings quite 
happily...



So, I am guessing that to convert strings to integers with int() that
the string must already be of integer format? What is the rationale
for setting up int() in this manner?


No idea, you'd need to ask Guido, it's his language :-)

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get the structure values from a c file

2012-12-17 Thread Alan Gauld

On 17/12/12 03:12, Marefe Serentas wrote:


I want to create a python script that will store the value of the
structure /MainStruct/ from the c file.


You will need to be a lot more specific about your requirements.
A structure is a data definition, it does not have a value.
It is like saying you want to store the value of char.
Secondly what do you mean by "from the C file"? Do you
mean you want extract the value from the C source file?
Or do you mean the data is stored elsewhere and you want
to use the definition in the C file to interpret the data?
In which case where/how is the data stored?


Suppose my c file looks like this:
--

snip

typedef struct MainStruct
{
}Main;

void generate()
{
 MainStruct myMain = {0};


Is this valid C?
I'd have expected either

struct MainStruct myMain

or

Main myMain

But my C is a bit rusty...
But if I'm right please post runnable code otherwise we are all guessing.



 myMain.var1.a = 1
 myMain.var1.b = 'a'
 myMain.var2 = 3
 myMain.var3[0] = -3
 myMain.var3[1] = 6
 myMain.var3[2] = 18
}




My python script accepts a c file as input.
How can I get the values of the structure?


I assume you really mean the values of the myMain variable?
But taking the C file as input makes no sense since C files are
normally compiled and the resulting binary file executed.
So taking the C file as input would imply you want to
parse the C source, which you can do more easily by just
reading it!


Can I call the function generate() in python script, but it doesn't have
a return statement, how can I get the values by calling the function?


You can't call generate() because its not compiled into executable form. 
All you have is a text file full of C source code. If you have access to 
the executable there are ways of calling the function

but since it always stores the same values there seems little point.


Help me, I'm really new in python.


Are you also new in C?
Why are you doing this, there is presumably a bigger problem behind this 
because as it stands it makes no sense.



All I could think is just parse the c file. But it seems that it's not a
good idea.


The best parser in this case is your brain.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor