Re: [Tutor] writing files using modules and functions

2012-11-13 Thread Alan Gauld

On 13/11/12 06:43, Rufino Beniga wrote:

def MatInv(arr,file):
 f = open('file.txt','w')
 f.write(arr)
 f.close()

So I'm trying to write a module that will take a matrix (arr) and write
it to a text file.
The above module is called MatrixIO.py

#first I import it

import MatrixIO

#Then i call the function MatInv with matrix(a) and file name (doc) as
the arguments
MatInv(a,doc)

It creates a file called txt and it shows only boxes. What am I doing wrong?


We can't tell until we see your code. Assuming its not more than 100 
lines or so why not post it? If it is more then try creating a smaller 
example...


The most likely thing is that the data is binary and when you display it 
the values do not map to printable values. You probably need to convert 
the array to strings before writing. But that's just a guess because I 
can't see what you are doing.



--
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] Questions about classes

2012-11-13 Thread Dave Angel
On 11/12/2012 09:49 PM, brandon w wrote:
> I have been trying to understand classes. I have been studying from a book
> I picked up recently.
> I have two questions about them.
>
> 1. I saw in the book an assignment written like this:
>
> class HumanBeing:
> def makeName(self, name):
>   *self.name = name*
> *
> *
> Why is it not written like this?:
>
> class HumanBeing:
> def makeName(self, name):
> *  name = self.name*
> *
> *

Presumably there are also other methods, including an __init__ one. 
That's where initial attributes are (or should be) assigned.

You don't say what else you *do* understand.  So I'll assume you know
what functions are, and how arguments are passed into parameters.  A
class method is very similar, except that it has an extra argument,
normally called 'self', which is usually passed in a funny way.

You know what a list is?  It's an instance of the list class, and it has
certain methods.  Some of those are done with special syntax, like [5],
while others are done with standard method syntax, like the third line
below:
 mylist = list( (3,4) )#this is usually shortcutted as [3,4]
 item = 42
 mylist.append(item)

mylist is an instance of list, and if you were to look at the class
definition of list, it'd have a method called append.  In this case, the
self parameter of that method refers to mylist (the object), and the
other parameter of that method refers to item.

A class that you write is usually like a collection, with a bunch of
data items (data attributes), and a bunch of methods (method attributes)
to manipulate them.  But unlike a list, they don't have to be uniform --
you define their behavior entirely.   And each time you create an
instance of that class, it gets its own set of attributes.  This
persistence of data between method calls is most of what makes the class
approach more powerful than functions.

Anyway, back to your example.

class HumanBeing:
def makeName(self, name):
  *self.name = name*

name is the second parameter, while self.name is an attribute on the current 
instance.  So if it's the latter you want changed, you'd better have it on the 
left side of the equals sign.


> 2. Why use a class in the first place? What is the purpose of constructing
> a class instead of just writing a program with a bunch of functions?
>
>
>

See above.  A class lets you collect multiple characteristics of a
"thing" (object) under one roof, along with the methods to manipulate
them.For the HumanBeing class, I'd have data attributes for things
like name, address, birthdate, bank_account_num.  And the individual
attributes might change over the life of the object, but they're all
kept together.

If you only had one HumanBeing to deal with in a given program, it
wouldn't matter much.  But if you have a lot of them, trying to use
global variables is very sloppy.

BTW, I wouldn't have a makeName() method, unless I had to start keeping
track of a person (infant) before they got their name.  But I very well
might have a changeName method, to be used at marriage and divorce, or
whenever a person went to court to have it changed.


-- 

DaveA

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


Re: [Tutor] writing files using modules and functions

2012-11-13 Thread Dave Angel
On 11/13/2012 01:43 AM, Rufino Beniga wrote:
> def MatInv(arr,file):
> f = open('file.txt','w')
> f.write(arr)
> f.close()
>
> So I'm trying to write a module that will take a matrix (arr) and write it
> to a text file.
> The above module is called MatrixIO.py
>
> #first I import it
>
> import MatrixIO
>
> #Then i call the function MatInv with matrix(a) and file name (doc) as the
> arguments
> MatInv(a,doc)
>
> It creates a file called txt and it shows only boxes. What am I doing wrong?
>
>

Before you try to figure out how it'll work in two modules, make it work
in one script.  Since you don't post enough code to actually run it,
we'd only be guessing why.  And even the code you show is wrong.  The
call to MatInv won't work with that import statement;  you'd need to
qualify it.

def matrix(values):
 return something??

import Matrix10
a = matrix(42)
doc = something else, perhaps a string literal
Matrix10.MatInv(a.doc)

So clearly you have some different source,  if it ever gets as far as
writing to the file.

Strip the code to a minimal test (pref. under 50 lines)
Specify Python version, OS type and version
Specify website and version for any non-standard library you import 
(eg. perhaps matrix)
Show the filenames and contents for all the source you supply
Show the full traceback of any error you get,   OR
Explain what you expected it to do, and how it was different

If I had to make a wild guess, I'd say that matrix was some form of
collection of floating point numbers.  But I have no idea what it
supplies to the write method, nor why one would expect that it should be
printable.  Might help to look at the docs for matrix.



-- 

DaveA

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


[Tutor] mapping list appends to correct position for csv output

2012-11-13 Thread Norman Khine
hello,
i am trying to create a csv file in python and map the fields to a
pre-existing fields, here is the title fileds of my csv


c = csv.writer(open("adm_products.csv", "wb"), delimiter='\t',
quotechar='"', quoting=csv.QUOTE_ALL)

import_fields = ["ID", "Active (0/1)", "Name *", "Categories (x,y,z...)",
"Price tax excl. or Price tax incl.", "Tax rules ID",
"Wholesale price", "On sale (0/1)",
"Discount amount", "Discount percent", "Discount from
(-mm-dd)", "Discount to (-mm-dd)",
"Reference #", "Supplier reference #", "Supplier",
"Manufacturer", "EAN13", "UPC", "Ecotax", "Weight",
"Quantity", "Short description", "Description", "Tags
(x,y,z...)", "Meta-title", "Meta-keywords",
"Meta-description", "URL rewritten", "Text when in
stock", "Text when backorder allowed",
"Available for order (0 = No, 1 = Yes)", "Product
creation date", "Show price (0 = No, 1 = Yes)",
"Image URLs (x,y,z...)", "Delete existing images (0 =
No, 1 = Yes)", "Feature(Name:Value:Position)",
"Available online only (0 = No, 1 = Yes)",
"Condition", "ID / Name of shop"]

so for example:

adm_product = []
for category in breadcrumbs.findAll('li', { "class" :
re.compile(r'\bcategory\d')}):
adm_product.append(category.find('a').renderContents()) # MAP TO CATEGORY

product_shop = soup.find('div', attrs={"class": "product-shop"})
product_sku = soup.find('p', attrs={"class": "product-sku"})
if product_sku:
sku = product_sku.renderContents()
product_ref = ref(sku)[0]
adm_product.append(product_ref) # MAP TO REFERENCE #

short_description = soup.find('div', attrs={"class": "short-description"})
if short_description:
short_desc = short_description.find('div', attrs={"class": "std"})
if short_desc:
adm_product.append(short_desc.renderContents()) # MAP TO SHORT
DESCRIPTION


What is the correct way to map the product_ref to the Reference # in
the import_fields list with any missing values being left blank or
create a csv so that when i append a value it is added to the correct
column?

also which is more efficient:

c.writerow(adm_product) # writing the product to the csv when all the
fileds are found

or

products = [] # adding them to a list first and then writing them to the csv
products.append(adm_product)
c.writerow(x) for x in products
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Questions about classes

2012-11-13 Thread Alan Gauld

On 13/11/12 02:49, brandon w wrote:


class HumanBeing:
 def makeName(self, name):
* self.name = name
*
*
Why is it not written like this?:

class HumanBeing:
 def makeName(self, name):
*  name = self.name


Because they two completely different things :-)

The first example takes an argument and assigns it to an attribute
of the object 'self'.

The second takes an argument and assigns the value of the object 
attribute to the argument (which will then get thrown away when the 
function exits)


When we use self.xxx we are accessing or storing that value in an object 
such that its value persists beyond the life of the function.
attributes of objects are a bit like module (or global) level variables 
except they are unique to a specific object. So whereas using global 
variables is considered bad practice using object attributes is good. 
You get the advantages of shared data without the problems of global names.



2. Why use a class in the first place? What is the purpose of
constructing a class instead of just writing a program with a bunch of
functions?


We write classes as a convenient way of encapsulating functions and data 
that we want to reuse, either within a single project or across 
projects. We reuse them by creating objects. It is the objects that are 
useful, classes are the mechanism for creating objects.


The problem with writing code purely with functions (and data) is the 
management of the data. Its fine if you only have a few data elements 
but when you start processing hundred, thousands or millions of data 
entities keeping them all stored separately and writing functions to 
access the data, making sure you don't corrupt one item while working on 
another becomes a real headache. If you now start parallel processing it 
gets even worse.


Just like modules are a way to control complexity and avoid data 
management issues classes do the same at a finer granularity.


Many people also find thinking about problems in terms of the objects 
involved more intuitive than separating the problem into functions and 
data. The real world is made up of objects that interact so it makes 
sense to build our software the same way. This is more effective in 
programs that model real world entities than in more abstract areas.
But it can be a real benefit in things like GUIs where we have objects 
like  windows, buttons, sliders, menus etc. Having code that reflects 
those visible objects makes GUI development much easier.


Even in the standard Python library we have string objects, file 
objects, and so on. Classes allow us to extend those concepts by 
creating our own objects.


They are optional though. You don't have to use them. You can achieve an 
awful lot without them. But often they make life easier, especially as 
your programs get bigger and more complex.


You'll find more detailed info and examples in the OOP topic of my tutorial.

HTH
--
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] functions and iterations

2012-11-13 Thread Alan Gauld

On 13/11/12 03:56, Rufino Beniga wrote:

def IterateLogistic(x,r,n):
 for i in xrange(n):
 x = r*(1-x)
 if i = n:
 print x




DogWalker has answered your basic question.

But you don't really need the test at all.
Just print x after the loop finishes:

def IterateLogistic(x,r,n):
  for i in xrange(n):
  x = r*(1-x)
  print x


But printing inside a function is usually not the best thing to do.
It's generally better practice to return the value and then print
the result externally:

def IterateLogistic(x,r,n):
  for i in xrange(n):
  x = r*(1-x)
  return x

print IterateLogistic(5,2,4)

It makes your function much more reusable. You can print the result or 
store it in a variable for later, or even use it directly in a bigger 
more complex expression.


HTH
--
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] mapping list appends to correct position for csv output

2012-11-13 Thread Alan Gauld

On 13/11/12 09:50, Norman Khine wrote:


also which is more efficient:

c.writerow(adm_product) # writing the product to the csv when all the
fileds are found

or

products = [] # adding them to a list first and then writing them to the csv
products.append(adm_product)
c.writerow(x) for x in products


Don't guess, measure. Write a small test and compare.
timeit is your friend.


--
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] Questions about classes

2012-11-13 Thread Steven D'Aprano

On 13/11/12 13:49, brandon w wrote:



1. I saw in the book an assignment written like this:

class HumanBeing:
 def makeName(self, name):
   *self.name = name*
*
*
Why is it not written like this?:

class HumanBeing:
 def makeName(self, name):
*  name = self.name*


This creates a class called "HumanBeing". It includes a method called
"makeName". Methods are very similar to functions, the differences will
become clear further on.

Methods are defined in the same way as functions: the "def" keyword,
followed by the name of method, then the parameters. Each parameter
creates a local variable, so the "makeName" method has two local
variables:

- self
- name

"self" is special. When you call the method, Python will automatically
provide the "self" argument. So if you do this:

fred = HumanBeing()  # create an instance of HumanBeing class
fred.makeName("flintstone")

the makeName method gets passed two arguments:

- self = fred, provided automatically by Python
- name = "flintstone", provided by you


Now, inside the body of the method, we have this:

self.name = name

That says:

- take the argument *name* (which has value "flintstone")
- attach it to the instance *self* using the attribute called "name"

After the line finishes executing, the instance *fred* will now have
an attribute *name* with value "flintstone".

So if you later call:

print(fred.name)

Python will print "flintstone".

What if it were written the other way, as you suggested?

name = self.name

That goes in the opposite direction: first Python tries to look up
an attribute called name. It probably doesn't find one, and so it
will raise an exception and print an error message. But let's
suppose it did find one. It then takes that value and stores it
in the local variable "name", over-writing the local variable you
provided as an argument to the method.

Then, when the method returns (either at a "return" statement, or
by reaching the end of the method), the local variable is
forgotten and no permanent change is made.




2. Why use a class in the first place? What is the purpose of constructing
a class instead of just writing a program with a bunch of functions?


Classes are useful for a couple of reasons:

1) Encapsulation

A class keeps together related code and data. A good example comes from the
Python built-in class "list". The list class combines:

- a storage area for the list data;
- methods which operate on that list data.

For example, lists have a method "index". But strings also have a method
called "index". The list.index method knows how to search a list. It knows
nothing about strings, and doesn't need to care about strings. It only
needs to care about lists. The str.list method knows how to search a string.
It knows nothing about lists, and only cares about strings. That makes it
much easier to program. Instead of one giant function:


def index(obj, value):
if obj is a string:
code for searching strings
elif obj is a list:
code for searching lists
elif obj is a tuple:
code for searching tuples
else:
raise TypeError("don't know how to index obj")

instead each branch of the function gets encapsulated into a str class, a
list class, a tuple class, and anything else that you might want to index.
If you write a Book class, you can give it an index method without needing
to care about lists, strings, tuples, etc.


The other advantage of classes is:

2) Inheritance

With classes, you can *inherit* behaviour by creating a subclass. Say, for
example, you want a type of list that is exactly the same as ordinary lists
except that every time you append a value, it prints what you appended. This
might be useful for debugging. Without inheritance, you would have to
duplicate the entire code base for list, many hundreds or thousands of lines
of code. But with inheritance, it just takes FOUR lines:

class MyList(list):
def append(self, value):
print("appending %s" % value)
super(MyList, self).append(value)


This creates a new class called "MyList", which inherits from the built-in
list class; everything else is the same as list, except for the append
method, which prints the value first, then calls the built-in list.append
method.

(If the super() call looks a bit mysterious, don't worry too much about it
right now.)



So between encapsulation and inheritance, classes are a powerful tool for
programming.



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


Re: [Tutor] Parsing a multi-line/record text file

2012-11-13 Thread wolfrage8...@gmail.com
On Sun, Nov 11, 2012 at 6:01 AM, Marc  wrote:

> **
>
> Hello,
>
> I am trying to parse a text file with a structure that looks like:
>
> [record: Some text about the record]
>
> Attribute 1 = Attribute 1 text
>
> Attribute 3 = Attribute 3 text
>
> Attribute 4 = Attribute 4 text
>
> Attribute 7 = Attribute 7 text
>
> [record: Some text about the record]
>
> Attribute 1 = Attribute 1 text
>
> 
>
> Etc…for many hundreds of records
>

It looks like a Config or INI file to me.  It is worth a try to see if
configparser is able to process the file, if it can you have a solution, if
not you only wasted a few minutes trying.

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


[Tutor] Reusing Timers (threading.timer)

2012-11-13 Thread Patrick Dickey
Hi everyone,

I've got an application that will use a timer to run a function
automatically (it's an update function for my IPv6 endpoint). The
questions that I have are these:

1.  Can I stop and start the timer from different functions or methods
in my program, and if so, how?
2.  Can I stop the timer, change the value, and restart it (or would it
create a new timer), or do I have to create a new timer with an entirely
new name?
3.  If I get a value from a textbox, how do I parse it from the string
value to an integer (or float)?
4.  Is there a better way of accomplishing this task?

Here's the pseudocode for what I'm doing.

if autoUpdates is enabled
   get updateFrequency
   start timer with time value from updateFrequency
   when time is reached, run update method
else 
   cancel timer

if autoUpdates is enabled AND user changes updateFrequency
   stop timer
   get updateFrequency
   start timer with new time value from updateFrequency

The autoUpdates and updateFrequency are a checkbox and text box in
wxPython.

Thanks for any advice on this, and have a great day.:)
Patrick.


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


Re: [Tutor] Reusing Timers (threading.timer)

2012-11-13 Thread Alan Gauld

On 13/11/12 15:42, Patrick Dickey wrote:

> 1.  Can I stop and start the timer from different functions or methods
> in my program, and if so, how?
> 2.  Can I stop the timer, change the value, and restart it (or would it
> create a new timer), or do I have to create a new timer with an entirely
> new name?

I can't help with threading.timer since I've never used it but...


3.  If I get a value from a textbox, how do I parse it from the string
value to an integer (or float)?


Use int() or float() ?


4.  Is there a better way of accomplishing this task?


Possibly. If you are using wxPython there is a timer (wx.Timer) in there 
that will fire an event (EVT_TIMER) after a suitable delay. This moves 
the timed event into your main processing code rather than having it in 
a thread. That might be easier. You can certainly start/stop the timer. 
You can recreate the timer with a new value after stopping. You can 
reuse the same variable name to store the timer.


HTH

--
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] Questions about classes

2012-11-13 Thread Ciaran Mooney
Hi,
Was hoping someone could help me. 
I have downloaded the latest pygame 1.9.1 i think) to a Mac powerbook OS 
10.4.11. 

Python 3 does not recognise pygame although python 2.7 version does 
(unfortunately have never programmed python 2.7 and don't no how).

Any help would be much appreciated.

Thanks
Ciaran



On 13 Nov 2012, at 12:57, Steven D'Aprano  wrote:

> On 13/11/12 13:49, brandon w wrote:
> 
> 
>> 1. I saw in the book an assignment written like this:
>> 
>> class HumanBeing:
>> def makeName(self, name):
>>   *self.name = name*
>> *
>> *
>> Why is it not written like this?:
>> 
>> class HumanBeing:
>> def makeName(self, name):
>> *  name = self.name*
> 
> This creates a class called "HumanBeing". It includes a method called
> "makeName". Methods are very similar to functions, the differences will
> become clear further on.
> 
> Methods are defined in the same way as functions: the "def" keyword,
> followed by the name of method, then the parameters. Each parameter
> creates a local variable, so the "makeName" method has two local
> variables:
> 
> - self
> - name
> 
> "self" is special. When you call the method, Python will automatically
> provide the "self" argument. So if you do this:
> 
> fred = HumanBeing()  # create an instance of HumanBeing class
> fred.makeName("flintstone")
> 
> the makeName method gets passed two arguments:
> 
> - self = fred, provided automatically by Python
> - name = "flintstone", provided by you
> 
> 
> Now, inside the body of the method, we have this:
> 
> self.name = name
> 
> That says:
> 
> - take the argument *name* (which has value "flintstone")
> - attach it to the instance *self* using the attribute called "name"
> 
> After the line finishes executing, the instance *fred* will now have
> an attribute *name* with value "flintstone".
> 
> So if you later call:
> 
> print(fred.name)
> 
> Python will print "flintstone".
> 
> What if it were written the other way, as you suggested?
> 
> name = self.name
> 
> That goes in the opposite direction: first Python tries to look up
> an attribute called name. It probably doesn't find one, and so it
> will raise an exception and print an error message. But let's
> suppose it did find one. It then takes that value and stores it
> in the local variable "name", over-writing the local variable you
> provided as an argument to the method.
> 
> Then, when the method returns (either at a "return" statement, or
> by reaching the end of the method), the local variable is
> forgotten and no permanent change is made.
> 
> 
> 
>> 2. Why use a class in the first place? What is the purpose of constructing
>> a class instead of just writing a program with a bunch of functions?
> 
> Classes are useful for a couple of reasons:
> 
> 1) Encapsulation
> 
> A class keeps together related code and data. A good example comes from the
> Python built-in class "list". The list class combines:
> 
> - a storage area for the list data;
> - methods which operate on that list data.
> 
> For example, lists have a method "index". But strings also have a method
> called "index". The list.index method knows how to search a list. It knows
> nothing about strings, and doesn't need to care about strings. It only
> needs to care about lists. The str.list method knows how to search a string.
> It knows nothing about lists, and only cares about strings. That makes it
> much easier to program. Instead of one giant function:
> 
> 
> def index(obj, value):
>if obj is a string:
>code for searching strings
>elif obj is a list:
>code for searching lists
>elif obj is a tuple:
>code for searching tuples
>else:
>raise TypeError("don't know how to index obj")
> 
> instead each branch of the function gets encapsulated into a str class, a
> list class, a tuple class, and anything else that you might want to index.
> If you write a Book class, you can give it an index method without needing
> to care about lists, strings, tuples, etc.
> 
> 
> The other advantage of classes is:
> 
> 2) Inheritance
> 
> With classes, you can *inherit* behaviour by creating a subclass. Say, for
> example, you want a type of list that is exactly the same as ordinary lists
> except that every time you append a value, it prints what you appended. This
> might be useful for debugging. Without inheritance, you would have to
> duplicate the entire code base for list, many hundreds or thousands of lines
> of code. But with inheritance, it just takes FOUR lines:
> 
> class MyList(list):
>def append(self, value):
>print("appending %s" % value)
>super(MyList, self).append(value)
> 
> 
> This creates a new class called "MyList", which inherits from the built-in
> list class; everything else is the same as list, except for the append
> method, which prints the value first, then calls the built-in list.append
> method.
> 
> (If the super() call looks a bit mysterious, don't worry too much abou

Re: [Tutor] Questions about classes

2012-11-13 Thread Steven D'Aprano

On 14/11/12 09:50, Ciaran Mooney wrote:

Hi,
Was hoping someone could help me.
I have downloaded the latest pygame 1.9.1 i think) to a Mac powerbook OS 
10.4.11.


What does this question have to do with classes?

When starting a brand new discussion, start with a brand new email thread:

* do NOT hit reply to an existing thread

* instead use your email program to create a new, fresh email

* set the TO address to tutor@python.org

* set the subject line to something appropriate, e.g. "Does Python 3 recognise 
pygame?"

* type your question

* hit SEND

PyGame and Python 3 is a frequently asked question. If you google for "pygame python 
3",
you will find plenty of answers to your question. E.g.:

http://www.pygame.org/wiki/FrequentlyAskedQuestions?#Does%20Pygame%20work%20with%20Python%203?
http://florian-berger.de/en/articles/installing-pygame-for-python-3-on-os-x


Please have a look at some of those, and if you are still not sure how to get 
pygame
working with Python 3, please come back with some specific questions.




Good luck,



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


Re: [Tutor] Reusing Timers (threading.timer)

2012-11-13 Thread Steven D'Aprano

On 14/11/12 02:42, Patrick Dickey wrote:

Hi everyone,

I've got an application that will use a timer to run a function
automatically (it's an update function for my IPv6 endpoint). The
questions that I have are these:


Have you read the documentation?

http://docs.python.org/2/library/threading.html#timer-objects


1.  Can I stop and start the timer from different functions or methods
in my program, and if so, how?


As the docs say:

[quote]
Timers are started, as with threads, by calling their start() method.
The timer can be stopped (before its action has begun) by calling
the cancel() method.
[end quote]

Any function or method that has access to the timer object can call
the start or cancel method. You just need to make sure that the
function can see the timer object, using one of the standard ways
in Python of giving a function access to any other object:

* (best) pass the object into the function as an argument

  result = myfunction(a, b, c, some_timer)

* (easiest) make the timer a global variable

* put the timer inside a dict, list or other container and
  pass it to the function

* put the timer in an attribute of the object

etc.




2.  Can I stop the timer, change the value, and restart it (or would it
create a new timer), or do I have to create a new timer with an entirely
new name?


Changing the timer is not supported. I expect that calling my_timer.start()
after cancelling it would restart it, but haven't tested it.

Just because you create a new timer doesn't mean you have to give it an
entirely new name.



3.  If I get a value from a textbox, how do I parse it from the string
value to an integer (or float)?


This has nothing to do with timers, and should go into a separate email
thread so that those people who know nothing about threading can contribute.

Regardless of where the string comes from, you turn a string into an int or
float by calling the int() or float() function.



4.  Is there a better way of accomplishing this task?

Here's the pseudocode for what I'm doing.

if autoUpdates is enabled
get updateFrequency
start timer with time value from updateFrequency
when time is reached, run update method
else
cancel timer



Well that won't work, because once the update has run, it will stop
checking for new updates.



if autoUpdates is enabled AND user changes updateFrequency
stop timer
get updateFrequency
start timer with new time value from updateFrequency


Seems fine to me.



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


[Tutor] data analysis with python

2012-11-13 Thread David Martins
Hi All

I'm trying to use python for analysing data from building energy simulations 
and was wondering whether there is way to do this without using anything sql 
like. 

The simulations are typically run for a full year, every hour, i.e. there are 
8760 rows and about 100+ variables such as external air temperature, internal 
air temperature, humidity, heating load, ... making roughly a million data 
points. I've got the data in a csv file and also managed to write it in a 
sqlite db.

I would like to make requests like the following:

Show the number of hours the aircon is running at 10%, 20%, ..., 100%
Show me the average, min, max air temperature, humidity, solar gains, when 
the aircon is running at 10%, 20%,...,100%

Eventually I'd also like to generate an automated html or pdf report with 
graphs. Creating graphs is actually somewhat essential.

I tried sql  and find it horrible, error prone, too much to write, the logic 
somehow seems to work different than my brain and I couldn't find particulary 
good documentation (particulary the documentation of the api is terrible, in my 
humble opinion). I heard about zope db which might be an alternative. Would you 
mind pointing me towards an appropriate way to solve my problem? Is there a way 
for me to avoid having to learn sql or am I doomed?

Thank you
dm



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


Re: [Tutor] data analysis with python

2012-11-13 Thread Andre' Walker-Loud
Hi David,

> I'm trying to use python for analysing data from building energy simulations 
> and was wondering whether there is way to do this without using anything sql 
> like. 
> 
> The simulations are typically run for a full year, every hour, i.e. there are 
> 8760 rows and about 100+ variables such as external air temperature, internal 
> air temperature, humidity, heating load, ... making roughly a million data 
> points. I've got the data in a csv file and also managed to write it in a 
> sqlite db.
> 
> I would like to make requests like the following:
> 
> Show the number of hours the aircon is running at 10%, 20%, ..., 100%
> Show me the average, min, max air temperature, humidity, solar gains, 
> when the aircon is running at 10%, 20%,...,100%
> 
> Eventually I'd also like to generate an automated html or pdf report with 
> graphs. Creating graphs is actually somewhat essential.
> 
> I tried sql  and find it horrible, error prone, too much to write, the logic 
> somehow seems to work different than my brain and I couldn't find particulary 
> good documentation (particulary the documentation of the api is terrible, in 
> my humble opinion). I heard about zope db which might be an alternative. 
> Would you mind pointing me towards an appropriate way to solve my problem? Is 
> there a way for me to avoid having to learn sql or am I doomed?


I would recommend learning 

hdf5http://www.hdfgroup.org/HDF5/

and the python utility to interface with it

pytableshttp://www.pytables.org/moin


and numpy and scipy are great for data analysis (python libraries) - numpy 
handles things like linear algebra, scipy has many built in scientific 
functions.  And then matplotlib for plotting (very similar functions to matlab 
if you are familiar with it).  Lastly, a very nice interface is "iPython", 
which is basically an enhanced python interpreter designed for/by science types.

All of these tools are installed for you with the Enthought Python Distribution 
(full dist is free if you have a .edu address, otherwise they provide a light 
version with basic libraries, and you can install others you like)

http://www.enthought.com/


If you have any specific questions on these (I know that is a lot to look into 
right away) let me know.


Cheers,

Andre






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


Re: [Tutor] data analysis with python

2012-11-13 Thread Ryan Waples
Not sure how stuck you are to python (I have no doubt it can tackle this)
but this is very much the sort of thing that 'R' is *really* good at.
Just FYI.
Good luck
Ryan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor