Re: [Tutor] How to reuse code in Python

2005-11-29 Thread Negroup -
2005/11/29, Kent Johnson <[EMAIL PROTECTED]>: > Negroup - wrote: [cut] > Obviously this doesn't do what you want. The problem is that class A is > seeing the limit defined in it's module. 'global' variables in Python > actually have module scope, there

[Tutor] How to reuse code in Python

2005-11-29 Thread Negroup -
Hi. Suppose that I need to use in my project a module that belongs to another project, because - with some tuning - it can fit my needings. This is what I mean, with an example: module.py limit = 30 class A: def __init__(self): self.num = 20 def info(self): return limit def inc_num

Re: [Tutor] Pretty XML

2005-11-24 Thread Negroup -
2005/11/23, Greg Lindstrom <[EMAIL PROTECTED]>: > something that would read in my XML file and format it in a similar manner > to "pretty print" so I can verify the correct information is being pulled. > My immediate concern is to read in an XML stream from a file and format it > with indentatio

Re: [Tutor] getattr of functions

2005-11-24 Thread Negroup -
2005/11/24, Liam Clarke <[EMAIL PROTECTED]>: > What do you mean enabled? > > If it's imported into the namespace you can call it... > > Err, can you clarify on the enabling, what context are you using it > in, and what are you trying to achieve? Hi, sorry. I'll try to present the actors omitting t

[Tutor] getattr of functions

2005-11-24 Thread Negroup -
Hi all! I'm here again with a question about introspection. My module stores a set of functions. I need to know, from another script, if a particular function of this module "is enabled" (it means, if it shall be executed by the caller script). I looked for some introspective builtin/function, but

[Tutor] Introspection (modules and functions)

2005-11-23 Thread Negroup -
Hi. My application hosts a module A that contains only a (variable) number of functions. In another part of this application I need to know which functions are defined inside module A. Initially I thought to use __dict__, but along with the functions of module A are listed all the builtins too. H

Re: [Tutor] Lists with just an item

2005-11-21 Thread Negroup -
2005/11/21, Alan Gauld <[EMAIL PROTECTED]>: > > Each dictionary has just one key and the corresponding > > value. > > > > structure = [{'field1': lenght1}, {'field2': lenght2}, ] > > I'd use a tuple rather than a dictionary here: Of course, this makes a lot of sense. [cut] > you can now

Re: [Tutor] Lists with just an item

2005-11-21 Thread Negroup -
2005/11/21, Brian van den Broek <[EMAIL PROTECTED]>: > Hi Negroup, > > why not just make structure a dict: > > structure = {'field1': lenght1, 'field2': lenght2} > > What does having a list of dicts all with a single key add but a layer > of r

[Tutor] Lists with just an item

2005-11-21 Thread Negroup -
Hi all. In my application I have chosen as data structure a list of dictionaries. Each dictionary has just one key and the corresponding value. structure = [{'field1': lenght1}, {'field2': lenght2}, ] Initially, to obtain the key and the value I used this code, for structure in record_st

Re: [Tutor] Walk a dictionary recursively

2005-10-11 Thread Negroup -
2005/10/11, paul brian <[EMAIL PROTECTED]>: > Firstly are you using this to store or alter data regarding Microsoft > Active Directory?. If so I suggest you look at some of their ADSI / > WMI interfaces for COM (if you use win32com from Mark Hammond or > activeState life becomes a lot easier. Well

[Tutor] Walk a dictionary recursively

2005-10-11 Thread Negroup -
ach key and value of the above descripted dictionary. Consider that the keys are integers or strings, and if a key is a list, I need to decode each contained element. Is there a way to walk recursively the dictionary, or should I write my own walk functio

Re: [Tutor] Reformatting a one (long) line xml file

2005-09-28 Thread Negroup -
2005/9/28, Christopher Arndt <[EMAIL PROTECTED]>: [cut] > > See http://www.python.org/doc/current/lib/module-xml.dom.minidom.html for more > info. Hi, this seems to be exactly what I was looking for! import sys from xml.dom import minidom INDENT = ' ' * 4 file = sys.argv[1] content = open(file

[Tutor] Reformatting a one (long) line xml file

2005-09-28 Thread Negroup -
Hi all, I have an xml file where the tags are all on the same line, without any newline. This file is quite big and difficult to read. I'd like to format it in a convenient way, using indentation and nesting. How to pretty print my content? I hope to have explained well enough my problem, differen

[Tutor] Pattern to handle exceptions

2005-09-23 Thread Negroup -
Hi all, I have a question about the following lines of code: >>> class Foo: ... bar = 'bar' ... >>> # is it better this >>> def te(attribute): ... try: ... print getattr(c, attribute) ... except AttributeError: ... return None >>> # >>> # or this? >>> def h_a(at

Re: [Tutor] Customized endswith/startswith version

2005-08-10 Thread Negroup -
2005/8/10, Michael Janssen <[EMAIL PROTECTED]>: > On 8/10/05, Negroup - <[EMAIL PROTECTED]> wrote: > > > >>> f = open('codes.txt') > > >>> # valid codes starts with 'abc' or '123' or 'ff5' > > >

[Tutor] Customized endswith/startswith version

2005-08-10 Thread Negroup -
Hi. >>> f = open('codes.txt') >>> # valid codes starts with 'abc' or '123' or 'ff5' >>> [valid for valid in f.readlines() if valid.startswith('abc') or valid.startswith('123') or valid.startswith('ff5')] This is just an example, in my application I don't need to check strings against a huge numbe

[Tutor] Question on classes/instances

2005-08-09 Thread Negroup -
Hi, Sorry for the subject a bit generic. This question comes from a simple program I'm doing. I could have a working solution, but considering my inexperience, I'm just asking if my approach is acceptable. This is a simple class to manage (actually to create and save) categories of things. Categor

Re: [Tutor] Basic class inheritance

2005-07-28 Thread Negroup -
[cut] > > Yes, that is the more modern way to do it for new-style classes. In the > original example, class A does not inherit from object so I used the older > style. > Thanks guys for your answers, it's clear (even if I wouldn't be able to figure it out by my own). About old style/new style

[Tutor] Basic class inheritance

2005-07-28 Thread Negroup -
I have this class: >>> class A: ... def __init__(self, blank=False, editable=True, name='foo'): ... self.blank = blank ... self.editable = editable ... self.name = name ... >>> a = A() >>> a.blank, a.editable, a.name (False, True, 'foo') All as expected. No

[Tutor] Questions on file.read

2005-07-14 Thread Negroup -
>>> help(f.read) Help on built-in function read: read(...) read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be return

Re: [Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Negroup -
2005/7/13, Sandip Bhattacharya <[EMAIL PROTECTED]>: > Negroup - wrote: [cut] > > Why dont you use python-sqlite to dump your data? You might need to > modify your sql slightly(depending on your current db), but rest is all sql. Thanks for the suggestion, I didn't even k

[Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Negroup -
Hi all, I hope my questions makes sense.. I'm trying to migrate a lot of data from an accounting system software to another using Python. Software A stores datas inside sql tables, software B too, with a different db structure. My idea has been to export sensible tables from db A, store them in py

[Tutor] Create zip files on the fly.

2005-07-07 Thread Negroup -
Hi, in my application I need to generate a zip file (via the great zipfile module) and pass it from function A to function B. One way to do it is create the object from function A and write it on filesystem via close(). Then, function B will obtain the object reading the file from disk. After this