Re: [Tutor] Multiples python files

2011-03-01 Thread Christopher Brookes
Thank you all for answers. Again.

2011/2/28 Alan Gauld 

>
> "Christopher Brookes"  wrote
>
>
>  I don't understand
>>
>> @classmethod
>>   def DisplayAll(cls, herosAll):
>>
>> What is cls ?
>>
>
> This is one of the advanced techniques I referred to a few days ago.
>
> Basically the "best"(?) solution to your problem is to store the list of
> characters inside the Character class as what is known as a class variable.
>
> Then everytime you create a new Character you can  add it to the list by
> the init method. And when a character is deleted you remove it via the del
> method.
>
> You can then define class methods to read/print the list, find out how many
> characters exist etc.
>
> This is much cleaner than keeping a separate global variable since the code
> for managing characters is all in one place with the classs definition. But
> it does introduce a bunch of new syntax features which I didn't think you
> were ready for yet! :-)
>
> One thing you will need to be very careful about as you go forward is
> namespaces. Remember that everytime you import a module you need to precede
> any names in that module with the module name. And names inside a class need
> to be preceded by the class name. And names inside objects need to be
> preceded by the object (variable) name.
> If the class is inside a module you need to use both module and class. Thus
> if the Character class is defined inside character.py you would have
> something like this in main.py:
>
> import character
> myObj = character.Character()
> print myObj.display()
> print character.Character.displayAll()
>
> etc.
>
> You can simplify it by using the "from m import v" style:
>
> from character import Character
> myObj = Character()
> myObj.display()
> Character.displayAll()
>
> etc.
>
> HTH,
>
> --
> Alan Gauld
> 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
>



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


[Tutor] Accessing a DLL from python

2011-03-01 Thread Hanlie Pretorius
Hi Python Tutors,

I'm using a storm water modelling program, EPA SWMM, to model the
hydrology and hydraulics of a study area.

SWMM reports its results in a binary (.out) file that contains the
results for each element in the model at each time step in the model
run. According to the SWMM interface manual
(http://www.epa.gov/ednnrmrl/models/swmm/swmm5_iface.zip), one can use
a DLL file to read the .out file. I want to do this so that I can read
the state of a river at a specific time and plot its water level in a
GIS.

After some searching on the Internet, I came across ctypes, which I
have tried. According to the SWMM manual:

[manual]
Opening the Output File
--
A function named OpenSwmmOutFile(outFile) that performs these tasks is
contained in the example code files that accompany this guide. The
argument to the function is the name of the binary output file. The
return value from the function is an integer code with the following
meanings:
0 - the run was successful
1 - the run was terminated with an error
2 - the output file could not be opened.
[/manual]

So, I tried the following python code:
[code]
In [14]: swmmdll = cdll.LoadLibrary("C:\\Hanlie\\model\\SWMM\\swmm5_0_018.dll")

In [15]: results_file="C:\\Hanlie\\model\\SWMM\\c83a_v0\\c83a_v0.3.out"

In [16]: open_file=swmmdll.OpenSwmmOutFile(results_file)

Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
  File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'OpenSwmmOutFile' not found
[/code]

Can anyone perhaps help me to access the functions in this DLL?

The manual also states:
[manual]
The following files are needed for applications that call functions
from the swmm5.dll library:
· swmm5.h for C/C++ applications
 · swmm5.bas for Visual Basic applications
 · swmm5.pas for Delphi applications.
[/manual]

And they give an example in C, Basic and Pascal to use the interface.
I have appended the C example to this message.

Thanks
Hanlie

[code]
// swmm5_iface.c
//
// Example code for interfacing SWMM 5 with C/C++ programs.
//
// Remember to #include the file swmm5_iface.h in the calling program.

#include 
#include 
#include "swmm5.h"

intSWMM_Nperiods;  // number of reporting periods
intSWMM_FlowUnits; // flow units code
intSWMM_Nsubcatch; // number of subcatchments
intSWMM_Nnodes;// number of drainage system nodes
intSWMM_Nlinks;// number of drainage system links
intSWMM_Npolluts;  // number of pollutants tracked
double SWMM_StartDate; // start date of simulation
intSWMM_ReportStep;// reporting time step (seconds)

intRunSwmmExe(char* cmdLine);
intRunSwmmDll(char* inpFile, char* rptFile, char* outFile);
intOpenSwmmOutFile(char* outFile);
intGetSwmmResult(int iType, int iIndex, int vIndex, int period,
float* value);
void   CloseSwmmOutFile(void);

static const int SUBCATCH = 0;
static const int NODE = 1;
static const int LINK = 2;
static const int SYS  = 3;
static const int RECORDSIZE = 4;   // number of bytes per file record

static int SubcatchVars;   // number of subcatch reporting variables
static int NodeVars;   // number of node reporting variables
static int LinkVars;   // number of link reporting variables
static int SysVars;// number of system reporting variables

static FILE*  Fout;// file handle
static intStartPos;// file position where results start
static intBytesPerPeriod;  // bytes used for results in each period
static void   ProcessMessages(void);

//-
int RunSwmmExe(char* cmdLine)
//-
{
  int exitCode;
  STARTUPINFO si;
  PROCESS_INFORMATION  pi;

  // --- initialize data structures
  memset(&si, 0, sizeof(si));
  memset(&pi, 0, sizeof(pi));
  si.cb = sizeof(si);
  si.wShowWindow = SW_SHOWNORMAL;

  // --- launch swmm5.exe
  exitCode = CreateProcess(NULL, cmdLine, NULL, NULL, 0,
 0, NULL, NULL, &si, &pi);

  // --- wait for program to end
  exitCode = WaitForSingleObject(pi.hProcess, INFINITE);

  // --- retrieve the error code produced by the program
  GetExitCodeProcess(pi.hProcess, &exitCode);

  // --- release handles
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  return exitCode;
}


//-
int RunSwmmDll(char* inpFile, char* rptFile, char* outFile)
//

Re: [Tutor] Accessing a DLL from python

2011-03-01 Thread Hanlie Pretorius
I see that I have misread the manual and that I need to reproduce the
code in the C example in python. The SWMM DLL doesn't contain
ready-made functions to open files etc.

Apologies for posting before I was completely sure of the problem.

Hanlie

2011/3/1, Hanlie Pretorius :
> Hi Python Tutors,
>
> I'm using a storm water modelling program, EPA SWMM, to model the
> hydrology and hydraulics of a study area.
>
> SWMM reports its results in a binary (.out) file that contains the
> results for each element in the model at each time step in the model
> run. According to the SWMM interface manual
> (http://www.epa.gov/ednnrmrl/models/swmm/swmm5_iface.zip), one can use
> a DLL file to read the .out file. I want to do this so that I can read
> the state of a river at a specific time and plot its water level in a
> GIS.
>
> After some searching on the Internet, I came across ctypes, which I
> have tried. According to the SWMM manual:
>
> [manual]
> Opening the Output File
> --
> A function named OpenSwmmOutFile(outFile) that performs these tasks is
> contained in the example code files that accompany this guide. The
> argument to the function is the name of the binary output file. The
> return value from the function is an integer code with the following
> meanings:
> 0 - the run was successful
> 1 - the run was terminated with an error
> 2 - the output file could not be opened.
> [/manual]
>
> So, I tried the following python code:
> [code]
> In [14]: swmmdll =
> cdll.LoadLibrary("C:\\Hanlie\\model\\SWMM\\swmm5_0_018.dll")
>
> In [15]: results_file="C:\\Hanlie\\model\\SWMM\\c83a_v0\\c83a_v0.3.out"
>
> In [16]: open_file=swmmdll.OpenSwmmOutFile(results_file)
> 
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
> func = self.__getitem__(name)
>   File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
> func = self._FuncPtr((name_or_ordinal, self))
> AttributeError: function 'OpenSwmmOutFile' not found
> [/code]
>
> Can anyone perhaps help me to access the functions in this DLL?
>
> The manual also states:
> [manual]
> The following files are needed for applications that call functions
> from the swmm5.dll library:
> · swmm5.h for C/C++ applications
>  · swmm5.bas for Visual Basic applications
>  · swmm5.pas for Delphi applications.
> [/manual]
>
> And they give an example in C, Basic and Pascal to use the interface.
> I have appended the C example to this message.
>
> Thanks
> Hanlie
>
> [code]
> // swmm5_iface.c
> //
> // Example code for interfacing SWMM 5 with C/C++ programs.
> //
> // Remember to #include the file swmm5_iface.h in the calling program.
>
> #include 
> #include 
> #include "swmm5.h"
>
> intSWMM_Nperiods;  // number of reporting periods
> intSWMM_FlowUnits; // flow units code
> intSWMM_Nsubcatch; // number of subcatchments
> intSWMM_Nnodes;// number of drainage system nodes
> intSWMM_Nlinks;// number of drainage system links
> intSWMM_Npolluts;  // number of pollutants tracked
> double SWMM_StartDate; // start date of simulation
> intSWMM_ReportStep;// reporting time step (seconds)
>
> intRunSwmmExe(char* cmdLine);
> intRunSwmmDll(char* inpFile, char* rptFile, char* outFile);
> intOpenSwmmOutFile(char* outFile);
> intGetSwmmResult(int iType, int iIndex, int vIndex, int period,
> float* value);
> void   CloseSwmmOutFile(void);
>
> static const int SUBCATCH = 0;
> static const int NODE = 1;
> static const int LINK = 2;
> static const int SYS  = 3;
> static const int RECORDSIZE = 4;   // number of bytes per file record
>
> static int SubcatchVars;   // number of subcatch reporting
> variables
> static int NodeVars;   // number of node reporting
> variables
> static int LinkVars;   // number of link reporting
> variables
> static int SysVars;// number of system reporting
> variables
>
> static FILE*  Fout;// file handle
> static intStartPos;// file position where results start
> static intBytesPerPeriod;  // bytes used for results in each
> period
> static void   ProcessMessages(void);
>
> //-
> int RunSwmmExe(char* cmdLine)
> //-
> {
>   int exitCode;
>   STARTUPINFO si;
>   PROCESS_INFORMATION  pi;
>
>   // --- initialize data structures
>   memset(&si, 0, sizeof(si));
>   memset(&pi, 0, sizeof(pi));
>   si.cb = sizeof(si);
>   si.wShowWindow = SW_SHOWNORMAL;
>
>   // --- launch swmm5.exe
>   exitCode = CreateProcess(NULL, cmdLine, NULL, NULL, 0,
>  

Re: [Tutor] Accessing a DLL from python

2011-03-01 Thread Hanlie Pretorius
Can anyone perhaps suggest the easiest way of translating the C code
into Python, bearing in mind that I'm rather a beginner?

Thanks
Hanlie

2011/3/1, Hanlie Pretorius :
> I see that I have misread the manual and that I need to reproduce the
> code in the C example in python. The SWMM DLL doesn't contain
> ready-made functions to open files etc.
>
> Apologies for posting before I was completely sure of the problem.
>
> Hanlie
>
> 2011/3/1, Hanlie Pretorius :
>> Hi Python Tutors,
>>
>> I'm using a storm water modelling program, EPA SWMM, to model the
>> hydrology and hydraulics of a study area.
>>
>> SWMM reports its results in a binary (.out) file that contains the
>> results for each element in the model at each time step in the model
>> run. According to the SWMM interface manual
>> (http://www.epa.gov/ednnrmrl/models/swmm/swmm5_iface.zip), one can use
>> a DLL file to read the .out file. I want to do this so that I can read
>> the state of a river at a specific time and plot its water level in a
>> GIS.
>>
>> After some searching on the Internet, I came across ctypes, which I
>> have tried. According to the SWMM manual:
>>
>> [manual]
>> Opening the Output File
>> --
>> A function named OpenSwmmOutFile(outFile) that performs these tasks is
>> contained in the example code files that accompany this guide. The
>> argument to the function is the name of the binary output file. The
>> return value from the function is an integer code with the following
>> meanings:
>> 0 - the run was successful
>> 1 - the run was terminated with an error
>> 2 - the output file could not be opened.
>> [/manual]
>>
>> So, I tried the following python code:
>> [code]
>> In [14]: swmmdll =
>> cdll.LoadLibrary("C:\\Hanlie\\model\\SWMM\\swmm5_0_018.dll")
>>
>> In [15]: results_file="C:\\Hanlie\\model\\SWMM\\c83a_v0\\c83a_v0.3.out"
>>
>> In [16]: open_file=swmmdll.OpenSwmmOutFile(results_file)
>> 
>> Traceback (most recent call last):
>>   File "", line 1, in 
>>   File "C:\Python26\lib\ctypes\__init__.py", line 366, in __getattr__
>> func = self.__getitem__(name)
>>   File "C:\Python26\lib\ctypes\__init__.py", line 371, in __getitem__
>> func = self._FuncPtr((name_or_ordinal, self))
>> AttributeError: function 'OpenSwmmOutFile' not found
>> [/code]
>>
>> Can anyone perhaps help me to access the functions in this DLL?
>>
>> The manual also states:
>> [manual]
>> The following files are needed for applications that call functions
>> from the swmm5.dll library:
>> · swmm5.h for C/C++ applications
>>  · swmm5.bas for Visual Basic applications
>>  · swmm5.pas for Delphi applications.
>> [/manual]
>>
>> And they give an example in C, Basic and Pascal to use the interface.
>> I have appended the C example to this message.
>>
>> Thanks
>> Hanlie
>>
>> [code]
>> // swmm5_iface.c
>> //
>> // Example code for interfacing SWMM 5 with C/C++ programs.
>> //
>> // Remember to #include the file swmm5_iface.h in the calling program.
>>
>> #include 
>> #include 
>> #include "swmm5.h"
>>
>> intSWMM_Nperiods;  // number of reporting periods
>> intSWMM_FlowUnits; // flow units code
>> intSWMM_Nsubcatch; // number of subcatchments
>> intSWMM_Nnodes;// number of drainage system nodes
>> intSWMM_Nlinks;// number of drainage system links
>> intSWMM_Npolluts;  // number of pollutants tracked
>> double SWMM_StartDate; // start date of simulation
>> intSWMM_ReportStep;// reporting time step (seconds)
>>
>> intRunSwmmExe(char* cmdLine);
>> intRunSwmmDll(char* inpFile, char* rptFile, char* outFile);
>> intOpenSwmmOutFile(char* outFile);
>> intGetSwmmResult(int iType, int iIndex, int vIndex, int period,
>> float* value);
>> void   CloseSwmmOutFile(void);
>>
>> static const int SUBCATCH = 0;
>> static const int NODE = 1;
>> static const int LINK = 2;
>> static const int SYS  = 3;
>> static const int RECORDSIZE = 4;   // number of bytes per file record
>>
>> static int SubcatchVars;   // number of subcatch reporting
>> variables
>> static int NodeVars;   // number of node reporting
>> variables
>> static int LinkVars;   // number of link reporting
>> variables
>> static int SysVars;// number of system reporting
>> variables
>>
>> static FILE*  Fout;// file handle
>> static intStartPos;// file position where results
>> start
>> static intBytesPerPeriod;  // bytes used for results in each
>> period
>> static void   ProcessMessages(void);
>>
>> //-
>> int RunSwmmExe(char* cmdLine)
>> //-
>> {
>>   int exitCode;

Re: [Tutor] Accessing a DLL from python

2011-03-01 Thread Stefan Behnel

Hanlie Pretorius, 01.03.2011 13:33:

Can anyone perhaps suggest the easiest way of translating the C code
into Python, bearing in mind that I'm rather a beginner?


A beginner of what? Python? Programming in general?

The C code you posted doesn't look too complex, so you could try to 
translate it (mostly literally) into Python syntax and use Cython to wrap 
that in a binary extension.


Cython is basically Python, but it allows you to call directly into C code. 
Here's a tutorial:


http://docs.cython.org/src/tutorial/clibraries.html

Stefan

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


[Tutor] couchdb.mapping 'Document' class

2011-03-01 Thread Emanuel Lauria
Hi everyone,

I'm trying to map a couchdb document to a python object using couchdb.mapping. 
I'm stuck in the very first part were it says I should declare a Python class 
that inherits from the 'Document'.. Where does this 'Document' superclass comes 
from? I can't resolve it. Or do I have to create it? How? Could someone point 
me in to the right direction?

>>> import couchdb

>>> class Person(Document):
... name = TextField()
... 
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'Document' is not defined


Thanks for your help, and please forgive me if im too n00b in this. I havent 
found any documentation except http://packages.python.org/CouchDB/mapping.html 
which is not enough.

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


Re: [Tutor] A class that instantiates conditionally ?

2011-03-01 Thread Alan Gauld


"David"  wrote

clever enough to refuse to instantiate itself if a necessary 
condition

is not met.




class MyClass_2(object):
def __new__(self, condition):
 if condition:
   return object.__new__(self)
 else:
   return None


Thats pretty much how I'd do it.

Alan G. 



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


Re: [Tutor] A class that instantiates conditionally ?

2011-03-01 Thread Alan Gauld


"David"  wrote

clever enough to refuse to instantiate itself if a necessary 
condition

is not met.


Oops, sent too soon.

I meant to add that you should realize that the implication of your
design is that the user of the class now has to check each object
to see if it is a valid reference or None. You could raise an 
exception

instead of returning None which allows a try/except style...

This extra overhead is one reason these kinds of "clever" tricks
are usually avoided. A valid object with null content is often
preferrable, or a singleton style pattern. But occasionally your
style is needed, just be aware of the extra overhead you
introduce by using it.

HTH,


--
Alan Gauld
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


[Tutor] Recommendation for Pygame

2011-03-01 Thread ANKUR AGGARWAL
Hey
Any good recommendation (ebook,links,videos,Live Example) to get started
with the pygame api. I am totally new to pygame.
Thanks in advance. Waiting for the suggestions :):)
Ankur Aggarwal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A class that instantiates conditionally ?

2011-03-01 Thread Hugo Arts
On Tue, Mar 1, 2011 at 5:35 PM, Alan Gauld  wrote:
>
> "David"  wrote
>
>> clever enough to refuse to instantiate itself if a necessary condition
>> is not met.
>
> Oops, sent too soon.
>
> I meant to add that you should realize that the implication of your
> design is that the user of the class now has to check each object
> to see if it is a valid reference or None. You could raise an exception
> instead of returning None which allows a try/except style...
>
> This extra overhead is one reason these kinds of "clever" tricks
> are usually avoided. A valid object with null content is often
> preferrable, or a singleton style pattern. But occasionally your
> style is needed, just be aware of the extra overhead you
> introduce by using it.
>

Side question: Any reason why you'd raise the exception from __new__
rather than __init__? If you want to return None, then yeah I can see
why you'd have to use __new__, but would there be any reason you can't
or shouldn't simply raise an exception from __init__?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recommendation for Pygame

2011-03-01 Thread Chris Fuller

http://www.pyweek.org/

Cheers

On Tuesday 01 March 2011, ANKUR AGGARWAL wrote:
> Hey
> Any good recommendation (ebook,links,videos,Live Example) to get started
> with the pygame api. I am totally new to pygame.
> Thanks in advance. Waiting for the suggestions :):)
> Ankur Aggarwal

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


Re: [Tutor] A class that instantiates conditionally ?

2011-03-01 Thread Alan Gauld


"Hugo Arts"  wrote


Side question: Any reason why you'd raise the exception from __new__
rather than __init__? If you want to return None, then yeah I can 
see
why you'd have to use __new__, but would there be any reason you 
can't

or shouldn't simply raise an exception from __init__?


Because by the  time you get to init you have already created
the instance. If you really want to avoid creating an instance it
needs to be in new.


Alan G


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


Re: [Tutor] Recommendation for Pygame

2011-03-01 Thread Alan Gauld


"ANKUR AGGARWAL"  wrote

Any good recommendation (ebook,links,videos,Live Example) to get 
started

with the pygame api. I am totally new to pygame.
Thanks in advance. Waiting for the suggestions :):)


Have you looked at the tutorial on the pygamje web site?

Also the Dietel book has a chapter on Pygame.
Expensive book but you might get it via a library?

Alan G. 



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


[Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Sean Carolan
Maybe someone can help with this.  I have a function that takes a
single file as an argument and outputs a tuple with each line of the
file as a string element.  This is part of a script that is intended
to concatenate lines in files, and output them to a different file.
This is as far as I've gotten:


import sys

myfiles = tuple(sys.argv[1:])
numfiles = len(myfiles)

def makeTuple(file):
outlist = []
f = open(file, 'r')
for line in f.readlines():
line = line.rstrip(' \n')
outlist.append(line)
return tuple(outlist)

for i in range(numfiles):
makeTuple(myfiles[i])


The script creates the tuples as it was intended to, but I'm not sure
how to assign variable names to each tuple so that I can work with
them afterwards.  How would you dynamically assign variable names to
each tuple created by makeTuple(), so that they can be manipulated
further?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Sean Carolan
On Tue, Mar 1, 2011 at 11:55 AM, Sean Carolan  wrote:
> Maybe someone can help with this.  I have a function that takes a
> single file as an argument and outputs a tuple with each line of the
> file as a string element.  This is part of a script that is intended
> to concatenate lines in files, and output them to a different file.

Not sure if this is the "right" or best way to do this, but I ended up
using vars() to assign my variable names, like so:

import sys

myfiles = tuple(sys.argv[1:])
numfiles = len(myfiles)
varlist = []

def makeTuple(file):
   6 lines:outlist = [] --

for i in range(numfiles):
varlist.append('tuple'+str(i))
vars()[varlist[i]] = makeTuple(myfiles[i])
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recommendation for pygame

2011-03-01 Thread Fernando Salamero
I teach videogames programming to my students using Python and Pygame:

http://pythonwiki.wikispaces.com


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


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Martin A. Brown

Sean,

 : Maybe someone can help with this.  I have a function that takes a 
 : single file as an argument and outputs a tuple with each line of 
 : the file as a string element.  This is part of a script that is 
 : intended to concatenate lines in files, and output them to a 
 : different file. This is as far as I've gotten:
 : 
 : 
 : import sys
 : 
 : myfiles = tuple(sys.argv[1:])
 : numfiles = len(myfiles)
 : 
 : def makeTuple(file):
 : outlist = []
 : f = open(file, 'r')
 : for line in f.readlines():
 : line = line.rstrip(' \n')
 : outlist.append(line)
 : return tuple(outlist)
 : 
 : for i in range(numfiles):
 : makeTuple(myfiles[i])
 : 
 : 
 : The script creates the tuples as it was intended to, but I'm not sure
 : how to assign variable names to each tuple so that I can work with
 : them afterwards.  How would you dynamically assign variable names to
 : each tuple created by makeTuple(), so that they can be manipulated
 : further?

Well, your function makeTuple() is returning something.  You could 
simply take the returned result and assign that to whatever variable 
you wish.

Some minor notes:

  * Your use of numFiles, i.e. 'for i in range(numFiles):' is 
not necessary, you can iterate directly over the file list.

  * You are using 'f.readlines()' which loads the whole file into 
memory (not that this is important in this example, since you 
are reading the whole file into memory, anyway).  Nonetheless, 
one wonderfully pythonic feature is that you can iterate
directly over File-like objects.  Try this:

   file = open(filename,'r')
   for line in file:
   # -- do something with each line

  * It seems a bit strange to me that you want to use a tuple for 
the contents of these files.  A list of lines in a file seems 
more natural (to me).  Tuples are hashable, which is quite 
convenient, but you can't modify them.

  * You use a tuple also for the list of files the user has passed.  
Why not just use the list you already have?  sys.argv

  * There are different camps on the creation of lists and 
dictionaries.  I find the English-like creation easier to read, 
so I always do:

  d = dict() # -- same as d = {}
  l = list() # -- same as l = []

Do you know how to use a dictionary?  It seems a natural for what it 
looks like you are trying to do (although you have not explained 
your intent, so can only guess).

  d = dict()
  for file in sys.argv[1:]:
  d[file] = makeTuple(file)

You then sent this:

  : for i in range(numfiles):
  :varlist.append('tuple'+str(i))
  :vars()[varlist[i]] = makeTuple(myfiles[i])

I saw in your follow-up that you went straight for vars().  I really 
don't think that's what you wish to use.  Get rid of vars(), he had 
to go to jail.  Don't go visit vars() again for at least two months, 
then maybe he'll be out on probation.

Adjusting your code (instead of the dictionary approach I suggested 
above), I'd suggest using your list variable directly!

  varlist = []
  for i in myfiles:
  varlist.append( makeTuple( i ) )

Good luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Hugo Arts
On Tue, Mar 1, 2011 at 7:10 PM, Sean Carolan  wrote:
> On Tue, Mar 1, 2011 at 11:55 AM, Sean Carolan  wrote:
>> Maybe someone can help with this.  I have a function that takes a
>> single file as an argument and outputs a tuple with each line of the
>> file as a string element.  This is part of a script that is intended
>> to concatenate lines in files, and output them to a different file.
>
> Not sure if this is the "right" or best way to do this, but I ended up
> using vars() to assign my variable names, like so:
>
> import sys
>
> myfiles = tuple(sys.argv[1:])
> numfiles = len(myfiles)
> varlist = []
>
> def makeTuple(file):
>   6 lines:    outlist = [] --
>
> for i in range(numfiles):
>    varlist.append('tuple'+str(i))
>    vars()[varlist[i]] = makeTuple(myfiles[i])

http://docs.python.org/library/functions.html#vars

As you can see in the documentation, you really shouldn't modify the
object returned by vars() or locals(). It might work in some cases for
some implementations of python, but it's actually an undefined
operation, which basically means that an implementation may do
anything it damn well pleases when you try to actually do it.

Really, you shouldn't be trying to dynamically add variables to the
namespace for each tuple, it's dangerous and can introduce all sorts
of hard-to-catch bugs. Instead, put all your tuples in a list, and
address them by index:

tuples = []
for file in myfiles:
tuples.append(makeTuple(file))

now you can address your tuples by tuples[0], tuples[1] and so forth,
which is pretty much the same as tuple0, tuple1, etc. Even better,
since it's in a list we can also iterate over it now with a for loop,
isn't that great?

Note also how I'm iterating over the myfiles list, which means I don't
have to use the range() function together with indexing with i, which
is a lot more readable.

HTH,
Hugo

PS: There's even shorter ways to write this little script, but I won't
bother you with them. If you want to know, check the map function, and
list comprehensions.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Sean Carolan
> I saw in your follow-up that you went straight for vars().  I really
> don't think that's what you wish to use.  Get rid of vars(), he had
> to go to jail.  Don't go visit vars() again for at least two months,
> then maybe he'll be out on probation.

Thanks Martin and Hugo.  As you can tell I'm no python guru.  Maybe I
should take a step back and explain exactly what it is I'm trying to
do.  I know this can be done quickly with awk or even perl but I want
to get more practice with python.  So here's the basic idea:

Take an arbitrary number of text files. Assume that each text file has
the exact same number of lines.  Concatenate each line of each file
with the corresponding lines of the other files and output the data.
So in other words, the first line of output will be
file1_line1+file2_line1+file3_line1, etc.

I'll work on this some more and see what I can come up with.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Joel Goldstick
On Tue, Mar 1, 2011 at 1:59 PM, Sean Carolan  wrote:

> > I saw in your follow-up that you went straight for vars().  I really
> > don't think that's what you wish to use.  Get rid of vars(), he had
> > to go to jail.  Don't go visit vars() again for at least two months,
> > then maybe he'll be out on probation.
>
> Thanks Martin and Hugo.  As you can tell I'm no python guru.  Maybe I
> should take a step back and explain exactly what it is I'm trying to
> do.  I know this can be done quickly with awk or even perl but I want
> to get more practice with python.  So here's the basic idea:
>
> Take an arbitrary number of text files. Assume that each text file has
> the exact same number of lines.  Concatenate each line of each file
> with the corresponding lines of the other files and output the data.
> So in other words, the first line of output will be
> file1_line1+file2_line1+file3_line1, etc.
>
> I'll work on this some more and see what I can come up with.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Just some quick ideas:

Read about Generators.  Using Hugo's snippet for reading a file a line at a
time, you can write a function to yield a single line of the file for each
call.  Do this for as many files as you are combining, and concatinate the
lines each pass.  Then write to your outfile

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


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Emile van Sebille

On 3/1/2011 10:59 AM Sean Carolan said...


Take an arbitrary number of text files. Assume that each text file has
the exact same number of lines.  Concatenate each line of each file
with the corresponding lines of the other files and output the data.
So in other words, the first line of output will be
file1_line1+file2_line1+file3_line1, etc.



Hint:  Look at zip.

Emile

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


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Hugo Arts
On Tue, Mar 1, 2011 at 7:59 PM, Sean Carolan  wrote:
>> I saw in your follow-up that you went straight for vars().  I really
>> don't think that's what you wish to use.  Get rid of vars(), he had
>> to go to jail.  Don't go visit vars() again for at least two months,
>> then maybe he'll be out on probation.
>
> Thanks Martin and Hugo.  As you can tell I'm no python guru.  Maybe I
> should take a step back and explain exactly what it is I'm trying to
> do.  I know this can be done quickly with awk or even perl but I want
> to get more practice with python.  So here's the basic idea:
>
> Take an arbitrary number of text files. Assume that each text file has
> the exact same number of lines.  Concatenate each line of each file
> with the corresponding lines of the other files and output the data.
> So in other words, the first line of output will be
> file1_line1+file2_line1+file3_line1, etc.
>
> I'll work on this some more and see what I can come up with.

My advice would be to go read up on the zip() function and the
str.join() function. Then, if you are using python 2.x, go find
itertools.izip. It does the same thing as zip but it's more memory
efficient. With those two you can do it in about two lines or so (and
maybe a few for set up and clarity and such).

If you get stuck, don't hesitate to ask.

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


[Tutor] File transfer HTTP -> SFTP

2011-03-01 Thread Emanuel Lauria
Sorry if im annoying, but I think this is a better question than my previous 
one of today.

I have some Images in an HTTP Server that I need to transfer to an SFTP server 
using a Python script.

So far I can download them to my hard drive, and upload them to the sftp 
server; quite easy. I would like to do the transfer without touching my hard 
drive.

How could I do that?

Im using paramiko, but STPClient.put() reads from the filesystem.

Thanks

put(self, localpath, remotepath, callback=None)
Copy a local file (localpath) to the SFTP server as remotepath.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Sean Carolan
> My advice would be to go read up on the zip() function and the
> str.join() function. Then, if you are using python 2.x, go find
> itertools.izip. It does the same thing as zip but it's more memory
> efficient. With those two you can do it in about two lines or so (and
> maybe a few for set up and clarity and such).

This is what I've got so far:

import sys
myfiles = sys.argv[1:]
for i in zip(open(myfiles[0]), open(myfiles[1]), open(myfiles[2])):
print " ".join(i)

How would you:

1.  zip an arbitrary number of files in this manner?  I hard-coded it
to do only three.
2.  Strip out trailing spaces and line breaks from the lines in each file?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Emile van Sebille

On 3/1/2011 11:49 AM Sean Carolan said...

My advice would be to go read up on the zip() function and the
str.join() function. Then, if you are using python 2.x, go find
itertools.izip. It does the same thing as zip but it's more memory
efficient. With those two you can do it in about two lines or so (and
maybe a few for set up and clarity and such).


This is what I've got so far:

import sys
myfiles = sys.argv[1:]
for i in zip(open(myfiles[0]), open(myfiles[1]), open(myfiles[2])):
 print " ".join(i)

How would you:

1.  zip an arbitrary number of files in this manner?  I hard-coded it
to do only three.


One way:

for i in zip([ open(filename) for filename in myfiles ])



2.  Strip out trailing spaces and line breaks from the lines in each file?


Convert the file contents before zip'ing -- so add

def cleanedup(filename):
return [ line.strip() for line in open(filename) ]

Then your loop looks like:

for i in zip([ cleanedup(filename) for filename in myfiles ])


HTH,

Emile

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


Re: [Tutor] couchdb.mapping 'Document' class

2011-03-01 Thread Knacktus

Am 01.03.2011 16:19, schrieb Emanuel Lauria:

Hi everyone,

I'm trying to map a couchdb document to a python object using
couchdb.mapping. I'm stuck in the very first part were it says I should
declare a Python class that inherits from the 'Document'.. Where does
this 'Document' superclass comes from? I can't resolve it. Or do I have
to create it? How? Could someone point me in to the right direction?

 >>> import couchdb

 >>> class Person(Document):
... name = TextField()
...
Traceback (most recent call last):
File "", line 1, in 
NameError: name 'Document' is not defined



It looks like the class Document is located in the module couchdb.client 
(it's chapter 3.3).

This should work:

import couchdb.client

class Person(couchdb.client.Document):

...

HTH

Jan



Thanks for your help, and please forgive me if im too n00b in this. I
havent found any documentation except
http://packages.python.org/CouchDB/mapping.html which is not enough.

Thanks



___
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] File transfer HTTP -> SFTP

2011-03-01 Thread Modulok
This isn't a python solution, but if you have an ssh connection to
both servers, (I assume so) you can forward them directly. It would
look like this in an operating system shell like tcsh or bash:

me@baz> scp -r m...@foo.com:/home/me/pictures m...@bar.com:/somewhere/

You could wrap the above command with python via the subprocess module
if needed.
-Modulok-


On 3/1/11, Emanuel Lauria  wrote:
> Sorry if im annoying, but I think this is a better question than my previous
> one of today.
>
> I have some Images in an HTTP Server that I need to transfer to an SFTP
> server using a Python script.
>
> So far I can download them to my hard drive, and upload them to the sftp
> server; quite easy. I would like to do the transfer without touching my hard
> drive.
>
> How could I do that?
>
> Im using paramiko, but STPClient.put() reads from the filesystem.
>
> Thanks
>
> put(self, localpath, remotepath, callback=None)
> Copy a local file (localpath) to the SFTP server as remotepath.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Steven D'Aprano

Emile van Sebille wrote:

On 3/1/2011 11:49 AM Sean Carolan said...

My advice would be to go read up on the zip() function and the
str.join() function. Then, if you are using python 2.x, go find
itertools.izip. It does the same thing as zip but it's more memory
efficient. With those two you can do it in about two lines or so (and
maybe a few for set up and clarity and such).


This is what I've got so far:

import sys
myfiles = sys.argv[1:]
for i in zip(open(myfiles[0]), open(myfiles[1]), open(myfiles[2])):
 print " ".join(i)

How would you:

1.  zip an arbitrary number of files in this manner?  I hard-coded it
to do only three.


One way:

for i in zip([ open(filename) for filename in myfiles ])


Almost, you need to expand the list:

zip( *[open(filename) for filename in myfiles] )

which is equivalent to pseudocode:

zip(open(myfiles[0]), open(myfiles[1]), ..., open(myfiles[N]))


Another way is:

zip(*map(open, myfiles))

Again, you need to expand the list using *.



2.  Strip out trailing spaces and line breaks from the lines in each 
file?


Convert the file contents before zip'ing -- so add

def cleanedup(filename):
return [ line.strip() for line in open(filename) ]


Better to do the stripping on demand, rather than all up front. 
Especially if the files are huge. Turn the list comprehension [...] into 
a generator expression (...):


def cleanedup(filename):
return (line.strip() for line in open(filename))



Then your loop looks like:

for i in zip([ cleanedup(filename) for filename in myfiles ])




--
Steven

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


Re: [Tutor] Dynamically assign variable names to tuple objects

2011-03-01 Thread Sean Carolan
> Another way is:
>
> zip(*map(open, myfiles))

>> Then your loop looks like:
>>
>> for i in zip([ cleanedup(filename) for filename in myfiles ])

Thanks, Steven!  I knew there was a way to do this with just a few
lines.  I will read up some more on list expansion and the map
built-in.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor